Jquery Load元素页面适用于除视频之外的所有内容

时间:2010-11-07 14:44:30

标签: jquery video

这是代码

$(function() {
    if(location.hash) $("#content_inload").load(location.hash.substring(1) + ' #content_inload'); 
    $("#nav a").click(function() {
        $("#content_inload").load(this.hash.substring(1) + ' #content_inload');
    });
});

出于某种原因,它会加载所有内容,但不会加载jw视频播放器。但是,如果我要求它加载整个页面而不仅仅是'#content_inload',它将加载视频,即使该页面没有头部分,没有脚本,也不会超过'#content_inload'内的内容。我只能假设JW播放器在内容顶部的底部添加了一行代码,但无法找到该行的内容。

1 个答案:

答案 0 :(得分:1)

如果你看到jquery的源代码关于他们做的.load()方法

...
self.html( 
    selector ?
    // Create a dummy div to hold the results
    jQuery("<div>")
        // inject the contents of the document in, removing the scripts
        // to avoid any 'Permission Denied' errors in IE
        .append(res.responseText.replace(rscript, ""))

        // Locate the specified elements
        .find(selector) :

    // If not, just inject the full result
    res.responseText 
);
...

因此,如果提供了一个选择器(就像你的情况一样),他们会删除脚本以避免IE中的Permission Denied错误。

您需要使用jquery .get()方法

模仿此代码

类似

$("#nav a").click(function() {
    $.get( this.hash.substring(1), function(response){
      var dummy = $('<div>').append( response ).find('#content_inload');
      $("#inload_container").html(dummy);
    } );
});

另请注意,在您将#inload_container插入其中时,我已使用#content_inload作为目标元素,实际上是复制..


<强>更新

在您发表评论后,我尝试了以下有效的工作

$("#nav a").click(function() {
    $.get( this.hash.substring(1), function(response){
      $("#inload_container").empty().append(response).find('>*:not(#content_inload)').remove();
    } );
});

在内存中的jquery对象中创建脚本元素时可能会出现问题? (无法确定原因

在你的特定情况下,上面的解决方案是可行的,但它不是很优雅,从某种意义上说,它将从ajax调用返回的所有内容添加到DOM中,然后过滤掉我们不想要的内容。

完全改变您的实际视频页面(从ajax 加载的视频页面)并执行正常的.load()而不过滤任何内容可能会更好..

相关问题