简体   繁体   English

在jquery中的span / div中加载javascript

[英]Loading javascript in Span/Div in jquery

I'm trying to load a javascript script link in div/span.(Script contains some image and link) script must come inside span when I run the page. 我正在尝试在div / span中加载一个javascript脚本链接。(该脚本包含一些图像和链接)脚本在运行页面时必须在span内。

I have tried with load(),$.getscript but unfortunately I was unable to do so.My script must run as soon as document load without any click or any user action. 我已经尝试过load(),$。getscript,但是不幸的是我没有这样做。我的脚本必须在文档加载后立即运行,而无需任何点击或任何用户操作。

HTML HTML

<span class=" loader"></span>

Jquery: jQuery的:

<script type="text/javascript">   

$.ajax({
  type: "GET",
  url: "test.js",
  dataType: "script"
  success: function( strHTML ){
$( ".loader" ).html( strHTML );
});

</script>

I tried this also: 我也尝试过这个:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home</title>
<script src="https://code.jquery.com/jquery-1.11.1.js" type="text/javascript"></script>
<style>

</style>

<script>
$(document).ready(function() 
     {
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.src = "test.js";
        // Use any selector
        $(".loader").append(s);
     });
     </script>
</head>
<body>
 <span class="loader">

  </span>
</body>


</html>

Any help will be helpful. 任何帮助都会有所帮助。

This may not be the right approach, but i tried like below and its working for me... 这可能不是正确的方法,但是我尝试了以下方法,并为我工作...

     $(document).ready(function() 
     {
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.src = "http://scriptlocation/test.js";
        // Use any selector
        $(".loader").append(s);
     });

Can you please refer the below link, it might be helpful Ajax/jQuery - Load webpage content into a div on page load? 您能否参考下面的链接,这可能会有所帮助Ajax / jQuery-在页面加载时将网页内容加载到div中?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM