简体   繁体   English

新动态创建(通过javascript)的html内容无法在头部看到以前使用的javascript文件

[英]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.

相关问题 如何在html头中动态添加javascript文件? - How to add javascript file dynamically in the head of html? 通过javascript与新创建的html交互 - interact with newly created html via javascript 中的Javascript <head> 的html文件不起作用 - Javascript in <head> of html file not working Javascript重定向到动态创建的HTML - Javascript redirect to dynamically created HTML 在 Javascript 中动态触发从服务器到用户的新创建文件(xlsx 或 docx)的下载 - Dynamically trigger download in Javascript for a newly created file (xlsx or docx) from Server to User 无法使用 JavaScript 输入搜索动态更改 HTML 内容? - Cannot change HTML content dynamically using JavaScript input search? 动态将值添加到新创建的下拉列表中,而不是以前创建的 - Dynamically add values to newly created dropdownlist instead of previously created android + javascript : 动态创建html并将javascript放在head标签中 - android + javascript : create html dynamically and put javascript in the head tag 无法将PHP文件加载到使用javascript动态创建的内部HTML Div中 - Unable to load PHP file into a inner HTML Div created dynamically with javascript 使用nodejs webserver在动态创建的html页面中执行javascript文件 - execute javascript file in dynamically created html page with nodejs webserver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM