[英]getting access to dom elements from inserted html with jquery
I want to get access to DOM-Elements which are added with jquery from an external HTML-File. 我想从外部HTML文件访问通过jquery添加的DOM元素。
Here's my first part of the code where I add the HTML: 这是我添加HTML的代码的第一部分:
$.get("resources/game/game_menu.html").success(function(data) {
$("#gameView2").html(data);
});
the "game_menu.html" looks just like this (atm for testing) “ game_menu.html”看起来像这样(用于测试的atm)
<div id="gameMenu" class="gameMenu">
<button id="startGame" type="button">Click Me!</button>
</div>
Now I'm trying to get access to the elements with jquery which looks like this: 现在,我正在尝试使用jquery来访问元素,如下所示:
$("#startGame").click(startGame());
$("#gameMenu").someFunction(someContext);
But both just don't work. 但是两者都不起作用。 How is possible to get access to ALL of the DOM-Elements.
如何获得对所有DOM元素的访问权限。 Can anyone help me?
谁能帮我?
Thx, myr0 Thx,myr0
You need to use $(document).on("click", "#startgame", startGame());
您需要使用
$(document).on("click", "#startgame", startGame());
for dynamically generated content. 用于动态生成的内容。
See Click event doesn't work on dynamically generated elements for the reason why you have to do this. 请参阅Click事件不适用于动态生成的元素 ,这是您必须执行此操作的原因。
Define the event inside 在内部定义事件
$.get("resources/game/game_menu.html").success(function(data) {
$("#gameView2").html(data);
$("#startGame").click(startGame());
});
Those elements not exist in DOM. 这些元素在DOM中不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.