|
|
挂过科的葡萄 · 将字符串按正则表达式分隔,忽略括号中的内容_ ...· 2 年前 · |
|
|
慷慨大方的野马 · Tutorial: Create an ...· 3 年前 · |
|
|
谈吐大方的香菜 · npm install Error: ...· 3 年前 · |
|
|
独立的树叶 · 从哈希到一致性哈希,一篇!不能再清晰了! ...· 3 年前 · |
你可以弹出警告框来显示数据:
< !DOCTYPE html >如需从 JavaScript 访问某个 HTML 元素,您可以使用 document.getElementById( id ) 方法。
请使用 "id" 属性来标识 HTML 元素,并 innerHTML 来获取或插入元素内容:
< !DOCTYPE html >以上 JavaScript 语句(在 <script> 标签中)可以在 web 浏览器中执行:
document.getElementById("demo") 是使用 id 属性来查找 HTML 元素的 JavaScript 代码
innerHTML = "段落已修改。" 是用于修改元素的 HTML 内容(innerHTML)的 JavaScript 代码。
上面的例子直接把 id="demo" 的 <p> 元素写到 HTML 文档输出中:
出于测试目的,您可以将JavaScript直接写在HTML
<
!DOCTYPE
html
>
<
html
>
<
body
>
<
h1
>
我的第一个 Web 页面
<
/h1
>
<
p
>
我的第一个段落。
<
/p
>
<
script
>
document.write(Date());
<
/script
>
<
/body
>
<
/html
>
尝试一下 »
大神
222***8080@qq.com
时妖
129***0814@qq.com
jingks
249***9451@qq.com
console.log 可以添加显示样式 eg:
console.log('%cfuck', 'font-size:20px')
我们用 IIFE 封装下:
(function() { var _log = console.log; console.log = function() { if (typeof(arguments[0]) != 'object' && typeof(arguments[0]) != 'function' && arguments.length == 1) { _log.call(console, '%c' + arguments[0], "text-shadow: 0 1px 0 #ccc,0 2px 0 #c9c9c9,0 3px 0 #bbb,0 4px 0 #b9b9b9,0 5px 0 #aaa,0 6px 1px rgba(0,0,0,.1),0 0 5px rgba(0,0,0,.1),0 1px 3px rgba(0,0,0,.3),0 3px 5px rgba(0,0,0,.2),0 5px 10px rgba(0,0,0,.25),0 10px 10px rgba(0,0,0,.2),0 20px 20px rgba(0,0,0,.15);font-size:5em") } else { _log.call(console, ...arguments)代码的意思是如果 console 只有一个参数,并且不是函数或者对象就用我们定义的样式,否则用 console 的默认输入。然后用油猴之类的插件在页面加载时就加载这个函数,就可以告别原来无聊的调试了,当然你也可以写一个样式库,写一个随机函数,这样每次 console 都会有惊喜了。
解决文档已完成加载后执行 document.write,整个 HTML 页面将被覆盖的问题
<button onclick="winTest()">按钮</button> function winTest() var txt1 = "This is a new window."; var txt2 = "This is a test."; document.open("text/html","replace");//加上 document.writeln(txt1); document.write(txt2); document.close();//加上点击 "按钮" 后页面显示结果:
This is a new window. This is a test.注:该方法将关闭 open() 方法打开的文档流,并强制地显示出所有缓存的输出内容。如果您使用 write() 方法动态地输出一个文档,必须记住当你这么做的时候要调用 close() 方法,以确保所有文档内容都能显示。document.write() 不会隐式调用 document.close() 方法的,否则例 2 中将不会有 This is a new window. 内容了
一旦调用了close(),就不应该再次调用 write(),因为这会隐式地调用 open() 来擦除当前文档并开始一个新的文档。
在载入页面后,浏览器输出流自动关闭。在此之后,比如延迟脚本 [setTimeout()] 或是 onload 执行的方法,任何一个对当前页面进行操作的 document.write()方法将打开—个新的输出流,它将清除当前页面内容(包括源文档的任何变量或值)。
陈飘
116***5073@qq.com
document.getElementByle() 和 document.write() 的区别
document.write() 方法可以用在两个方面:
1、 页面载入过程中用实时脚本创建页面内容,用来追加一些标签。 2、清除当前页面内容(包括源文档的任何变量或值)。重新生成内容。它的第 2 点和 document.getElementByle() 相区别。前者在清除全部页面内容,然后生成新的内容,即“覆盖文档"。
<script>
function myFunction()
document.write("糟糕,文档丢失了")
</script>
<button type="bulbon" onclick="myFunction()">覆盖文档</button>
点击按钮,括号里的内容则会全部覆盖文档。
<script>
document.getElementById("three").innerHTML="段落已修改。";
</script>
这个仅仅只是替换了 id 为 three 里的内容,没有覆盖页面。
白色羽毛
166***9869@qq.com
|
|
慷慨大方的野马 · Tutorial: Create an Angular app that uses the Microsoft identity platform for authentication using auth code flow - Microsoft Entr 3 年前 |
|
|
独立的树叶 · 从哈希到一致性哈希,一篇!不能再清晰了! - 墨天轮 3 年前 |