为什么当我在src属性中使用它时,jquery / javascript代码不起作用 [英] why jquery/javascript code doesn't work when I use it in same script tag with src attribute

查看:87
本文介绍了为什么当我在src属性中使用它时,jquery / javascript代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

脚本 - 带有src和content的标记是什么意思?

我在我的html页面中使用了以下内容:

 < html> 
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = ISO-8859-1>
< title>在此插入标题< / title>
< script type =text / javascriptsrc =resources / scripts / jquery-1.8.2.min.js>

$(document).ready(function(){
alert(ABC);
});
< / script>
< / head>

< body>
< p>这是我的索引页。< / p>
< / body>
< / html>

jquery在这里不起作用,这意味着在发出警报后我没有看到它的任何影响



但是,当我放置单独的< script> 标记时, / p>

 < html> 
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = ISO-8859-1>
< title>在此插入标题< / title>
< script type =text / javascriptsrc =resources / scripts / jquery-1.8.2.min.js>
< / script>
< script>
$(document).ready(function(){
alert(ABC);
});
< / script>
< / head>

< body>
< p>如果您点击我,我将消失。< / p>
< / body>
< / html>

所以如果有人有任何解释吗?

解决方案

这是因为您正在使用javascript提供src标记。

src文件的内容将作为< script> (开始和结束)标记的内容扩展。 / p>

如果需要,可以用萤火虫检查。


Possible Duplicate:
What does a script-Tag with src AND content mean?

I used the following in my html page

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="resources/scripts/jquery-1.8.2.min.js">

$(document).ready(function(){
  alert("ABC");
});
</script>
</head>

<body>
<p>THIS IS MY INDEX PAGE.</p>
</body>
</html>

The jquery doesn't work here meaning I don't see any effect of it after putting an alert in it.

But when I put in seperate <script> tag like the one below it works,

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="resources/scripts/jquery-1.8.2.min.js">
</script>
<script>
$(document).ready(function(){
  alert("ABC");
});
</script>
</head>

<body>
<p>If you click on me, I will disappear.</p>
</body>
</html>

so if anyone any explain?

解决方案

This is because, you are providing the src tag with the javascript.

The src file contents will get expanded as content of <script> (start and end) tag.

You can inspect this with firebug if you want.

这篇关于为什么当我在src属性中使用它时,jquery / javascript代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆