简体   繁体   English

使用JavaScript动态创建HTML页面时,jQuery脚本未加载到头部

[英]Jquery Script not loading in head when I create dynamically html page using javascript

I created dynamically new window in JavaScript and add some HTML code in it through JavaScript, but when I insert some script link into html head, it does not load when window is open. 我在JavaScript中动态创建了一个新窗口,并通过JavaScript在其中添加了一些HTML代码,但是当我在html head中插入一些脚本链接时,当窗口打开时它不会加载。

<script type="text/javascript">    
function newWindowMap(){

    var myWindow = window.open('','_blank');
    var head = myWindow.document.getElementsByTagName('head')[0];
    var body = myWindow.document.getElementsByTagName('body')[0];

    var jqueryScript = myWindow.document.createElement('script');
    jqueryScript.src = 'jquery.min.js';
    jqueryScript.type = "text/javascript";
    head.appendChild(jqueryScript);

    var alertScr = myWindow.document.createElement('script');
    var inlineScript = 
 document.createTextNode('$(document).ready(function(){alert("Hello World!");});');
    alertScr.appendChild(inlineScript);
    body.appendChild(alertScr);

}
 </script>

Error on console: 控制台错误:

Uncaught ReferenceError: $ is not defined at :1:1 未捕获的ReferenceError:$未定义为:1:1

$ is from JQuery, which is a popular library for JavaScript, and from the looks, you haven't imported it. $来自JQuery,它是JavaScript的流行库,从外观上来说,您尚未导入它。

Add this into your head tag to fix the issue 将此添加到您的头部标签即可解决此问题

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

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

相关问题 使用JavaScript在页面加载时动态创建HTML - Using JavaScript to dynamically create HTML on page load 将页面动态加载到jquery mobile时保持javascript完整 - leave javascript intact when dynamically loading a page into jquery mobile 如何在HTML页面停止后 <head> … </head> 使用JavaScript加载? - How can I stop a HTML page after the <head>…</head> loads using javascript? android + javascript : 动态创建html并将javascript放在head标签中 - android + javascript : create html dynamically and put javascript in the head tag 如何使用jquery / javascript从头部动态添加的脚本标签读取值 - How to read the values from the dynamically added script tag in head using jquery / javascript 如何在javascript的html头标记中动态添加脚本值 - how to add a script value dynamically in html head tag in javascript 我可以创建 html 元素吗<html> ,<head><body> 通过使用 CreateElement() 我 javascript? - can i create html elements like <html>,<head><body> by using CreateElement() i javascript? 使用jQuery,如何将动态添加的类删除到页面的头部 - using jQuery, how can I remove a dynamically added class to the head of a page 使用javascript或jquery动态地将Div添加到html页面 - Dynamically add Div to html page using javascript or jquery 如何使用javascript在html页面中动态创建元素? - How to create an element dynamically in an html page using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM