加载后Javascript不会执行 [英] Javascript won't execute after load

查看:86
本文介绍了加载后Javascript不会执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站包含链接,当我点击一下点击时,目标HTML内容使用JQuery加载到div中,到目前为止没问题。

My website contains links and when I click on a click, the target HTML content is loaded into a "div" using JQuery, no problem so far.

My问题是当我第三次加载下面的搜索页面时,脚本将不会执行:

My problem is when my search page below is loaded for the third time, the script will not execute:

<h1><span>Search</span></h1>
<p class="bigtext">Entity</p>
<input type="text" id="searchfield">
<p class="more">
    <a href="#">Find</a>
</p>
<p></p>
<div id="res"></div>

<script type="text/javascript">
    alert("hello");
</script>

观察脚本首次使用两次,但不是第三次。我也试过改变位置等,但没有积极的结果。问题只发生在搜索页面,HTML内容被加载,我也看到使用Firebug的脚本,但它不会执行alert()。

Observe that the script works for the two first times, but not for the third time. I have also tried changing the position etc, but no postive results. The problem only occurs for the search page, the HTML content is loaded, I also see the script using Firebug, but it won't execute the alert().

My一般load-into-div脚本是这样的:

My general load-into-div script is this:

<script type="text/javascript">
$(document).ready(function()
{
    var page = "blog";

    $('a').click(function()
    {
        var id = $(this).attr("id");
        if ( id != null && id !="" )
        {
            page = "index.php/"+id;
            $("#inc").load(page);
        }
    });

    setInterval(function() {
        if (page=="index.php/toplist")
            $("#inc").load(page);
    }, 6000);
});
</script>

有关如何解决此问题的任何建议?提前致谢。

Any suggestions on how to solve this problem? Thanks in advance.

// Hirre

推荐答案

加载HTML < script> 页面中的元素基本上不可靠。 jQuery需要采取一些步骤才能使其跨浏览器工作,但在某些情况下jQuery失败。

Loading HTML with <script> elements into the page is basically unreliable. jQuery takes a number of steps to try to make it work cross-browser, but in several cases jQuery fails.

所以不要依赖它。将脚本内容保留在基本脚本中,必要时在加载后从回调运行。

So don't rely on it. Keep your script content in the basic script instead, run from callback after loading if necessary.

这篇关于加载后Javascript不会执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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