jQuery文档()剔除了脚本标记 [英] jquery html() strips out script tags

查看:251
本文介绍了jQuery文档()剔除了脚本标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个Ajax调用了HTML产生的替换div的内容在我的网页。 的问题是,在html有在它的一些必要的脚本,似乎jquery的HTML()函数stripts出来, 我需要过滤的反应,只得到一个具体的股利。

I need to replace the content of a div in my page with the html resultant from an ajax call. The problem is that the html have some necessary scripts in it and it seems that jquery html() function stripts them out, I need to filter the response and only get a specific div.

我想上一个解决方法是提取Ajax响应所有的脚本标记,然后附加他们做的DOM,但我无法这样做。

I am thinking on a workaround which is to extract all the script tags from the ajax response and then append them do the DOM but i am having trouble doing that.

下面是我的code;

Here is my code;

   $('a.link-vote').live('click',function(){
        var idfeedback = $(this).attr('id').split('-')[1];
        var href = $(this).attr('href');
        $('.feedback-' + idfeedback + '-loader').show();
        $.ajax({
            type: "POST",
            url: href,
            success: function(response){
               var x = $(response).find('#feedback-'+ idfeedback).html();
               $('.feedback-' + idfeedback + '-loader').hide();
               $('#feedback-'+ idfeedback).html(x);

            }
        });
        return false;
    });

我发现这个老话题: <一href="http://stackoverflow.com/questions/2699320/jquery-script-tags-in-the-html-are-parsed-out-by-jquery-and-not-executed">http://stackoverflow.com/questions/2699320/jquery-script-tags-in-the-html-are-parsed-out-by-jquery-and-not-executed

I found this old topic: http://stackoverflow.com/questions/2699320/jquery-script-tags-in-the-html-are-parsed-out-by-jquery-and-not-executed

但任何结论。我试着推荐的解决方案,但他们没有工作。

but the is any conclusion. I tried the solutions suggested there but none of them work.

编辑: 我似乎发现基于该老话题一种解决方法,但it's不是pretty的;

I seem to found a workaround based on that old topic but it´s not pretty;

  var dom = $(response);
                // var x = $(response).find('#feedback-'+ idfeedback).html();
                $('.feedback-' + idfeedback + '-loader').hide();
                //$('#feedback-'+ idfeedback).html(x);

                $('#feedback-'+ idfeedback).html(dom.find('#feedback-'+ idfeedback).html());

                dom.filter('script').each(function(){
                    var obj = $(this);
                    $('#feedback-'+ idfeedback + ' .feedback-comments').append(obj);
                });

必须有一个简单的方法。

There must be a easy way.

推荐答案

编辑:我累了,没想到。你可以只使用本地的innerHTML 方法,而不是的.html()

I'm tired and not thinking. You can just use the native innerHTML method instead of .html():

$('#feedback-' + idfeedback)[0].innerHTML = x;


原来的答复:


Original answer:

我的直觉是,你链接的回答不适合你,因为包括脚本调用一个的src 属性,而不是<$ C $的脚本内容C>&LT;脚本&GT; 和&LT; / SCRIPT&GT; 标签。这可能会实现:

My hunch is that the answer you linked doesn't work for you because the included scripts are called with a src attribute rather than script content between the <script> and </script> tags. This might work:

$.ajax({
    url: 'example.html',
    type: 'GET',
    success: function(data) {

        var dom = $(data);

        dom.filter('script').each(function(){
            if(this.src) {
                var script = document.createElement('script'), i, attrName, attrValue, attrs = this.attributes;
                for(i = 0; i < attrs.length; i++) {
                    attrName = attrs[i].name;
                    attrValue = attrs[i].value;
                    script[attrName] = attrValue;
                }
                document.body.appendChild(script);
            } else {
                $.globalEval(this.text || this.textContent || this.innerHTML || '');
            }
        });

        $('#mydiv').html(dom.find('#something').html());

    }
});

请注意,这还没有测试任何东西,可以吃婴儿。

Note, this has not been tested for anything and may eat babies.

这篇关于jQuery文档()剔除了脚本标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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