简体   繁体   English

使用document.getElementById替换innerHTML时发生意外行为

[英]unexpected behaviour when replacing innerHTML using document.getElementById

What am I doing wrong in the following? 以下我在做什么错? I would like to replace only the content of a div, but the content of the whole page is changed. 我只想替换div的内容,但是整个页面的内容已更改。

Why is the link "Next" disappearing when "Second page" is displayed? 为什么显示“第二页”时,“下一步”链接消失了?

<div id = "notes">
 <p>First page</p>
</div>

<a href="javascript:document.getElementById('notes').innerHTML = '<h1>Second page</h1>'">Next</a>

Navigating to a javascript: URI will display the result of the script. 导航到javascript: URI将显示脚本的结果。

The assignment operator evaluates to the value assigned, so you end up navigating to that chunk of HTML directly. 赋值运算符计算赋值的值,因此您最终直接导航到该HTML块。

To prevent this, wrap your expression in the void() operator so that it will have no value. 为避免这种情况,请将表达式包装在void()运算符中,使其没有任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM