-1

我是 jquery 的新手,谁能告诉我如何在我的 html 文件中使用外部 jquery 显示和图像。

我希望我的 html 干净并加载外部 jquery 文件中提到图像路径的图像。

我的 jquery 文件会像这样 testjquery.js

 // url to load the image (which i dont know ,whats the code for it)

并在我的 html 文件中。

<html>
  <head>
    <script type="text/javascript">
        (function() {
            var test = document.createElement('script');
            test.type = 'text/javascript'; 
            test.async = true;
            test.src = 'testquery.js';
            var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(test, s);

            test.onload=function(){
                // function to load the external image
            }
         })();
    </script>
  </head>
  <body>
  </body>
</html>

提前致谢!

4

4 回答 4

0

我不知道你的意思是什么......但我猜你的意思是突兀的 jQuery。所以你的 html 保持干净。您可以将 javascript 放在外部文件中或页面底部。如果将其放在外部文件中,则可以删除脚本标签。

<html>
  <head>
    <script src="style.js" type="text/javascript"></script>
  </head>
  <body>
    <div id="image"></div>
  </body>
</html>

并在 style.js 中(可以放在同一个目录下)

  $(document).ready(function(){
    imagePath = 'your_image.png';
    $('#image').html("<img src='" + imagePath + "' />")
  });

您可以使用 html 或附加。html 函数清除整个图像 div 并将其替换为图像。追加,将其附加到 div 以便您以后可以添加另一个。(document).ready 函数将在整个文档加载后运行。

于 2012-07-12T07:30:54.217 回答
0

我认为你的问题是错误的
,所以测试一下

首先确定加载的 Jquery 脚本文件

并为 div 元素设置 html

<div id="imageContainer"></div>

<script>
    $('#imageContainer').html('<img src="/images/test.jpg" />');
</script>
于 2012-07-12T07:31:39.650 回答
0

您可以简单地使用:

HTML

<div id="divWhereYouWantAddImage"></div>

查询

$('#divWhereYouWantAddImage').append("<img src='img.jpg' />");

您可以在此处阅读有关 jquery.append()函数的更多信息。

于 2012-07-12T07:34:17.530 回答
0

下面是 ExternalJSFile.js 中的文件内容

imageUrl = 'your/img/path.png';

你的 html 文件

<html>  
</html>

<script src="ExternalJSFile.js" type="text/javascript"></script>

<script type="text/javascript">
 $(document).ready(function(){
     alert(imageUrl);
  });
</script>
于 2012-07-12T07:41:01.777 回答