简体   繁体   English

javascript onclick 更改 div 在类或内联 styles 内部时不起作用

[英]javascript onclick change div not working while classes or inline styles inside in html

i am using javascript to change html content, just for an example I have done something like below:我正在使用 javascript 来更改 html 内容,仅举个例子,我做了如下的事情:

 <div id="grid"> <div id="title"> <button onclick='document.getElementById("grid").innerHTML = "<p style="color:red">semma <b class="mate">Kite</b></p>"' style="background: linear-gradient(to bottom, #cc99ff 0%, #ff99cc 100%);"> <h3> Rice, Grains &amp; Flours</h3> </button> </div> </div>

this is not working because I am using a class and inline style in the onclick.这不起作用,因为我在 onclick 中使用 class 和内联样式。 can anyone please tell me how to fix this.谁能告诉我如何解决这个问题。 thanks in advance提前致谢

The problem is here: <button onclick='document.getElementById("grid").innerHTML = "<p style="color:red">semma <b class="mate">Kite</b></p>"' style="background: linear-gradient(to bottom, #cc99ff 0%, #ff99cc 100%);"> .问题出在这里: <button onclick='document.getElementById("grid").innerHTML = "<p style="color:red">semma <b class="mate">Kite</b></p>"' style="background: linear-gradient(to bottom, #cc99ff 0%, #ff99cc 100%);"> . You did not escape the double quotes.您没有转义双引号。 The correct HTML for the button would be this:按钮的正确 HTML 是这样的:

<button onclick='document.getElementById("grid").innerHTML = "<p style=\"color:red\">semma <b class=\"mate\">Kite</b></p>"' style="background: linear-gradient(to bottom, #cc99ff 0%, #ff99cc 100%);">

 <div id="grid"> <div id="title"> <button onclick='document.getElementById("grid").innerHTML = "<p style=\"color:red\">semma <b class=\"mate\">Kite</b></p>"' style="background: linear-gradient(to bottom, #cc99ff 0%, #ff99cc 100%);"> <h3> Rice, Grains &amp; Flours</h3> </button> </div> </div>

You should comment out the quotes inside \"您应该注释掉\"中的引号

 <div id="grid"> <div id="title"> <button onclick='document.getElementById("grid").innerHTML = "<p style=\"color:red\">semma <b class=\"mate\">Kite</b></p>"' style="background: linear-gradient(to bottom, #cc99ff 0%, #ff99cc 100%);"> <h3> Rice, Grains &amp; Flours</h3> </button> </div> </div>

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

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