在变量JS中使用onclick =“myFunction()”(Use onclick=“myFunction()” in Variable JS)

编程入门 行业动态 更新时间:2024-10-10 06:20:51
变量JS中使用onclick =“myFunction()”(Use onclick=“myFunction()” in Variable JS)

我有一个div ,其中onclick="myFunction()"为' 显示/隐藏 ' id="myTable" 。

file.js中我使用这样的格式:

function example () { var popup = '<div class="box">' + '<div onclick="myFunction()" id="button"></div>' + '<div id="myTable" class="myTable"></div>' + '</div>'; //box }; //End function example() /**** How to make myFunction () work? ****/ function myFunction() { var x = document.getElementById("myTable"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } }; // End function myFunction()

我想使用 onclick="myFunction()"来显示和隐藏 div id myTable 。

如果在w3school上使用html格式,这是有效的。

但我使用.js文件格式。 我不能添加HTML代码,而不是使用变量

var ex = '<htmlcode></htmlcode>';

W3Schools切换显示/隐藏

I have a div which has onclick="myFunction()" for 'Show / hide' id="myTable".

in file.js I use format like this :

function example () { var popup = '<div class="box">' + '<div onclick="myFunction()" id="button"></div>' + '<div id="myTable" class="myTable"></div>' + '</div>'; //box }; //End function example() /**** How to make myFunction () work? ****/ function myFunction() { var x = document.getElementById("myTable"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } }; // End function myFunction()

I want to Show and hide div id myTable using onclick="myFunction()" .

If using html format on w3school, this works.

But I use the .js file format. I cant add the html code other than using the variable

var ex = '<htmlcode></htmlcode>';

W3Schools Toggle Show/Hide

最满意答案

<!DOCTYPE html> <html> <head> <title>Demo example</title> </head> <body> <div id="hello"></div> <script> example(); function example() { var popup = '<div class="box">HELLO World' + '<button id="button">Button</button>' + '<div id="myTable" class="myTable">Some Content Here</div>' + '</div>'; //box var el = document.getElementById('hello') el.innerHTML = popup; //alert(popup); }; //End function example() /**** How to make myFunction () work? ****/ document.addEventListener('click', function(e) { console.log(e.target.id) if (e.target && e.target.id == 'button') { //do something myFunction() } }) function myFunction() { var x = document.getElementById("myTable"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } }; </script> </body> </html>

使用此代码片段通过使用此文档中的此片段.addEventListener将侦听所触发的所有点击,但仅当id与e.target.id中提供的指定ID匹配时更新样式值,通过此方法,您的js将这里是动态创建的元素。在你的情况下,onclick函数不会被触发,因为js没有链接在页面中动态创建的元素,并且通过事件委托我们可以解决这个问题。

<!DOCTYPE html> <html> <head> <title>Demo example</title> </head> <body> <div id="hello"></div> <script> example(); function example() { var popup = '<div class="box">HELLO World' + '<button id="button">Button</button>' + '<div id="myTable" class="myTable">Some Content Here</div>' + '</div>'; //box var el = document.getElementById('hello') el.innerHTML = popup; //alert(popup); }; //End function example() /**** How to make myFunction () work? ****/ document.addEventListener('click', function(e) { console.log(e.target.id) if (e.target && e.target.id == 'button') { //do something myFunction() } }) function myFunction() { var x = document.getElementById("myTable"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } }; </script> </body> </html>

Use this code snippet by making use of this snippet in this document.addEventListener will listen to all the clicks that are triggered but update the style value only when id matches the designated id provided in e.target.id , by this approach your js will here the dynamically created elements.In your case onclick function is not triggred because js have not linked the element dynamically created in the page , and by event delegation we can solve this problem.

更多推荐

本文发布于:2023-08-03 14:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1388960.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   JS   onclick   myFunction   Variable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!