简体   繁体   English

无法在aspx页面中调用外部javascript

[英]Unable to call external javascript in aspx page

I am a newbie to jquery. 我是jquery的新手。 Help me out to solve this thing 帮我解决这个问题

I have this script in a separate js file 我在单独的js文件中有此脚本

$(document).ready(function () {
$("#btn_click").click(function () {
    $("#SecondLine").hide();
});
});

I want to execute this script in a click on the element with id="btn_click" 我想通过单击id =“ btn_click”的元素来执行此脚本

<form id="form1">
<div>
    <h2>This is a heading</h2>
    <p>This is a paragraph.</p>
    <p id="SecondLine">This is another paragraph.</p>       
    <p1 id="btn_click">Click me</p1>
</div>
</form>

The code works perfectly when i include the whole script between the head tag on the same aspx page. 当我在同一个aspx页面的head标签之间包含整个脚本时,代码可以完美工作。 but it is not working when i have this code in a separate js file. 但是当我在单独的js文件中有此代码时,它不起作用。 Note: I have included the script in aspx page. 注意:我已将脚本包含在aspx页面中。

<script src="../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="../JQuery/JQuery_HidingText.js" type="text/javascript"></script>

Still not working :( .Did i Missed anything ? 仍然无法正常工作:(。我错过了什么吗?

You're load jQuery scripy twice. 您要加载jQuery scripy两次。

<script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

Remove one of them. 删除其中之一。

You should remove this file too: 您也应该删除此文件:

<script src="../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>

After that check paths to scripts. 之后,检查脚本路径。 Your script work correctly . 您的脚本可以正常工作。

Use ~ symbol instead .. 使用〜符号代替..

~/Scripts/jquery-1.4.1.js" instead of ../Scripts/jquery-1.4.1.js" ~/Scripts/jquery-1.4.1.min.js" instead of ../Scripts/jquery-1.4.1.min.js" 〜/ Scripts / jquery-1.4.1.js“代替../Scripts/jquery-1.4.1.js”〜/ Scripts / jquery-1.4.1.min.js“代替../Scripts/jquery- 1.4.1.min.js”

All the Best 祝一切顺利

Try using 尝试使用

pageLoad()

instead of 代替

$(document).ready(function ()
{
  //.. etc
}); 

The pageLoad uses a different method (timeouts) to check whether the DOM is ready. pageLoad使用其他方法(超时)来检查DOM是否准备就绪。 I had a script that failed with $(document).ready but worked with pageLoad - pageLoad hitting a fraction later which made the difference. 我有一个脚本因$(document).ready失败,但与pageLoad一起使用pageLoad稍后击中了一个分数,这有所作为。 This was only an issue in IE for me 对我来说,这只是IE中的一个问题

Anyway might not work but easy to try and worth a go i reckon 无论如何,可能不起作用,但很容易尝试,值得一试

See here for details pageLoad 详细信息请参见此处

And of course good luck 当然,祝你好运

Another thought 另一个想法

Change the location of the external script file reference to just before the closing body tag rather than in the head. 将外部脚本文件引用的位置更改为刚好在body标记之前,而不是在头部。 Again to give the markup the best chance of loading before the script executes (i know the document ready should do this but ...). 再次为标记提供在脚本执行之前加载的最佳机会(我知道准备就绪的文档应该这样做,但是...)。 Again worth a go 再次值得一游

And

Just for test purpose i would change the external script file to be as simple as possible just to rule any other wierdness out ie use 仅出于测试目的,我将外部脚本文件更改为尽可能简单,以排除任何其他问题,即使用

$(document).ready(function () {
    alert('Hello - i am here');
});

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

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