[英]newly dynamically created (by javascript) html content cannot see the previously used javascript file in the head
I have a link ( A link ), which dynamically creates some html content into the page by a js file placed in the head content. 我有一个链接( A link ),该链接通过放置在头部内容中的js文件在页面中动态创建一些html内容。 This works well. 这很好。
<head>
<script src="my.js">
</head>
<body>
<div>
<a href="A link">
</div>
</body>
Than clicked the A link, and this is created: 比单击A链接,将创建以下链接:
<div>
<a href="B link">
</div>
The newly created html content also contains a link ( B link ), which should use the same js file, as used before, but it seems, that the B link cannot see it, however the js file is still in the header content. 新创建的html内容还包含一个链接( B链接 ),该链接应使用与以前使用的相同的js文件,但似乎B链接看不到它,但是js文件仍位于标题内容中。
Works only if I put the js file in a script tag to the end of the dynamically created html content generated by A link , like this. 仅当我将js文件放在脚本标签中时才有效,这样由A link生成的动态创建的html内容的末尾。
<div>
<a href="B link">
<script src="my.js">
</div>
But this means I load this js file twice. 但这意味着我两次加载此js文件。 What am I missing here? 我在这里想念什么?
Thanks. 谢谢。
If you want the newly created element to perform some action then you should use this technique using jquery. 如果您希望新创建的元素执行某些操作,则应通过jquery使用此技术。
$(document).on('click', '.linkclass', function()
{
// Perform your action here.
alert('I was clicked');
});
live of Jquery is now obselete and is better to use on . jQuery的活 ,现在已废弃,并更好地使用上 。 In this way your all newly created element or already created elements having class as .linkclass will perform alert(); 这样,您所有新创建的元素或已经创建的具有.linkclass类的元素都将执行alert(); action. 行动。 Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.