ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 浏览器对象 弹出框 ``` <pre class="calibre14">``` <span class="token1">alert</span><span class="token2">(</span><span class="token3">1</span><span class="token2">)</span><span class="token2">;</span> var ret <span class="token">=</span> <span class="token1">prompt</span><span class="token2">(</span><span class="token4">"请输入内容!"</span><span class="token2">,</span><span class="token4">""</span><span class="token2">)</span><span class="token2">;</span> var v <span class="token">=</span> <span class="token1">confirm</span><span class="token2">(</span><span class="token4">'此操作不可恢复,确认吗?'</span><span class="token2">)</span><span class="token2">;</span> 返回<span class="token3">true</span>可操作 window<span class="token2">.</span><span class="token1">close</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span>关闭当前窗口 window<span class="token2">.</span><span class="token1">print</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span>打印 ``` ``` 计时器 ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">fn</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span> var d<span class="token">=</span><span class="token5">new</span> <span class="token1">Date</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> var t<span class="token">=</span>d<span class="token2">.</span><span class="token1">toLocaleTimeString</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> document<span class="token2">.</span><span class="token1">getElementById</span><span class="token2">(</span><span class="token4">"a1"</span><span class="token2">)</span><span class="token2">.</span>innerHTML<span class="token">=</span>t<span class="token2">;</span> <span class="token2">}</span> var t <span class="token">=</span> <span class="token1">setTimeout</span><span class="token2">(</span><span class="token4">"fn()"</span><span class="token2">,</span><span class="token3">5000</span><span class="token2">)</span><span class="token2">;</span> 执行一次 <span class="token1">clearTimeout</span><span class="token2">(</span>t<span class="token2">)</span><span class="token2">;</span> 提前结束 var id<span class="token">=</span><span class="token1">setInterval</span><span class="token2">(</span><span class="token4">"fn()"</span><span class="token2">,</span><span class="token3">1000</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//每1秒执行一次</span> <span class="token1">clearInterval</span><span class="token2">(</span>id<span class="token2">)</span><span class="token2">;</span> 取消定时 ``` ``` - 读写localStorage,长时间存储 sessionStorage关闭后删除 ``` <pre class="calibre14">``` localStorage<span class="token2">.</span><span class="token1">setItem</span><span class="token2">(</span><span class="token4">"key1"</span><span class="token2">,</span> <span class="token4">"123"</span><span class="token2">)</span><span class="token2">;</span> localStorage<span class="token2">.</span><span class="token1">removeItem</span><span class="token2">(</span><span class="token4">"bar"</span><span class="token2">)</span><span class="token2">;</span> var foo <span class="token">=</span> localStorage<span class="token2">.</span><span class="token1">getItem</span><span class="token2">(</span><span class="token4">"bar"</span><span class="token2">)</span><span class="token2">;</span> ``` ``` - 选择器 ``` <pre class="calibre14">``` var id<span class="token">=</span>document<span class="token2">.</span><span class="token1">getElementById</span><span class="token2">(</span><span class="token4">"id"</span><span class="token2">)</span><span class="token2">;</span> var id <span class="token">=</span> document<span class="token2">.</span><span class="token1">querySelector</span><span class="token2">(</span><span class="token4">"#k"</span><span class="token2">)</span><span class="token2">;</span> 支持标签<span class="token2">,</span><span class="token2">.</span>class<span class="token2">,</span>a<span class="token2">[</span>target<span class="token">=</span>_blank<span class="token2">]</span>属性 var id <span class="token">=</span> document<span class="token2">.</span><span class="token1">querySelectorAll</span><span class="token2">(</span><span class="token4">"li"</span><span class="token2">)</span><span class="token2">;</span> ``` ``` - window ``` <pre class="calibre14">``` document<span class="token2">.</span><span class="token1">write</span><span class="token2">(</span><span class="token4">"123"</span><span class="token2">)</span> 输出 console<span class="token2">.</span><span class="token1">log</span><span class="token2">(</span><span class="token4">"控制台"</span><span class="token2">)</span> var url <span class="token">=</span> location<span class="token2">.</span>href<span class="token2">;</span> location<span class="token2">.</span>href<span class="token">=</span><span class="token4">'http://www.baidu.com'</span><span class="token2">;</span> var domain <span class="token">=</span> document<span class="token2">.</span>domain<span class="token2">;</span><span class="token6">//不含http</span> var url <span class="token">=</span> <span class="token2">(</span>window<span class="token2">.</span>location<span class="token2">.</span>href<span class="token2">)</span><span class="token2">.</span><span class="token1">substring</span><span class="token2">(</span><span class="token3">0</span><span class="token2">,</span><span class="token2">(</span>window<span class="token2">.</span>location<span class="token2">.</span>href<span class="token2">)</span><span class="token2">.</span><span class="token1">indexOf</span><span class="token2">(</span>document<span class="token2">.</span>location<span class="token2">.</span>pathname<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//包含http</span> var from <span class="token">=</span> document<span class="token2">.</span>referrer<span class="token2">;</span>来源 location<span class="token2">.</span><span class="token1">reload</span><span class="token2">(</span><span class="token3">true</span><span class="token2">)</span><span class="token2">;</span>刷新 history<span class="token2">.</span><span class="token1">go</span><span class="token2">(</span><span class="token3">0</span><span class="token2">)</span><span class="token2">;</span> history<span class="token2">.</span><span class="token1">go</span><span class="token2">(</span><span class="token3">1</span><span class="token2">)</span><span class="token2">;</span>前进 history<span class="token2">.</span><span class="token1">go</span><span class="token2">(</span><span class="token">-</span><span class="token3">1</span><span class="token2">)</span><span class="token2">;</span>后退 document<span class="token2">.</span>title<span class="token2">;</span> document<span class="token2">.</span>title <span class="token">=</span> <span class="token4">"标题"</span><span class="token2">;</span> document<span class="token2">.</span><span class="token1">querySelector</span><span class="token2">(</span><span class="token4">'#c'</span><span class="token2">)</span><span class="token2">.</span>dataset<span class="token2">.</span>sex <span class="token">=</span> <span class="token4">"男"</span><span class="token2">;</span>data属性 document<span class="token2">.</span><span class="token1">querySelector</span><span class="token2">(</span><span class="token4">'#c'</span><span class="token2">)</span><span class="token2">.</span>dataset<span class="token2">.</span>sex<span class="token2">;</span> document<span class="token2">.</span><span class="token1">getElementById</span><span class="token2">(</span><span class="token4">'demo1'</span><span class="token2">)</span><span class="token2">.</span><span class="token1">setAttribute</span><span class="token2">(</span><span class="token4">'role'</span><span class="token2">,</span> <span class="token4">'button'</span><span class="token2">)</span><span class="token2">;</span> 设置获取属性 document<span class="token2">.</span><span class="token1">getElementById</span><span class="token2">(</span><span class="token4">'foo'</span><span class="token2">)</span><span class="token2">.</span><span class="token1">removeAttribute</span><span class="token2">(</span><span class="token4">'role'</span><span class="token2">)</span><span class="token2">;</span> document<span class="token2">.</span><span class="token1">getElementById</span><span class="token2">(</span><span class="token4">'foo'</span><span class="token2">)</span><span class="token2">.</span>innerHTML <span class="token">=</span> <span class="token4">'Goodbye!'</span><span class="token2">;</span>设置值 ``` ``` - 逻辑 ``` <pre class="calibre14">``` <span class="token5">if</span> <span class="token2">(</span>time<span class="token"><</span><span class="token3">10</span><span class="token2">)</span> <span class="token2">{</span> document<span class="token2">.</span><span class="token1">write</span><span class="token2">(</span><span class="token4">"<b>早上好</b>"</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token5">if</span> <span class="token2">(</span>time<span class="token">>=</span><span class="token3">10</span> <span class="token">&&</span> time<span class="token"><</span><span class="token3">16</span><span class="token2">)</span> <span class="token2">{</span> document<span class="token2">.</span><span class="token1">write</span><span class="token2">(</span><span class="token4">"<b>今天好</b>"</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token2">{</span> document<span class="token2">.</span><span class="token1">write</span><span class="token2">(</span><span class="token4">"<b>晚上好!</b>"</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token1">switch</span><span class="token2">(</span>n<span class="token2">)</span> <span class="token2">{</span> case <span class="token3">1</span><span class="token2">:</span> 执行代码块 <span class="token3">1</span> <span class="token5">break</span><span class="token2">;</span> case <span class="token3">2</span><span class="token2">:</span> 执行代码块 <span class="token3">2</span> <span class="token5">break</span><span class="token2">;</span> default<span class="token2">:</span> 与 case <span class="token3">1</span> 和 case <span class="token3">2</span> 不同时执行的代码 <span class="token2">}</span> <span class="token5">for</span> <span class="token2">(</span>var i<span class="token">=</span><span class="token3">0</span><span class="token2">;</span>i<span class="token"><</span>cars<span class="token2">.</span>length<span class="token2">;</span>i<span class="token">++</span><span class="token2">)</span> <span class="token2">{</span> document<span class="token2">.</span><span class="token1">write</span><span class="token2">(</span>cars<span class="token2">[</span>i<span class="token2">]</span> <span class="token">+</span> <span class="token4">"<br>"</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">while</span> <span class="token2">(</span>条件<span class="token2">)</span> <span class="token2">{</span> 需要执行的代码 <span class="token2">}</span> ``` ``` 监听事件 ``` <pre class="calibre16">``` var el<span class="token">=</span> document<span class="token2">.</span><span class="token1">querySelector</span><span class="token2">(</span><span class="token4">'#btn'</span><span class="token2">)</span><span class="token2">;</span> el<span class="token2">.</span><span class="token1">addEventListener</span><span class="token2">(</span><span class="token4">'click'</span><span class="token2">,</span><span class="token5">function</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span> document<span class="token2">.</span><span class="token1">getElementById</span><span class="token2">(</span><span class="token4">'loading1'</span><span class="token2">)</span><span class="token2">.</span>classList<span class="token2">.</span><span class="token1">add</span><span class="token2">(</span><span class="token4">'loading1'</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">)</span> ``` ```