【问题标题】:Extract html tags and div class from a variable containg the HTML of external page从包含外部页面 HTML 的变量中提取 html 标签和 div 类
【发布时间】:2013-12-25 20:03:09
【问题描述】:

此代码获取外部 URL 的 HTML 并将其传递给变量:text,然后使用包含在变量中的 html 向页面发出警报。我需要将元素与变量分开。

document.getelement 仅适用于当前页面中的 html。还有其他方法吗

即 text.document.getelementbyid('MyDiv')

test script<script type="text/javascript">
var your_url = 'http://google.com;

</script>

<script type="text/javascript" src="jquery.min.js" ></script>
<script type="text/javascript">
// jquery.xdomainajax.js  ------ from padolsey

jQuery.ajax = (function(_ajax){

var protocol = location.protocol,
    hostname = location.hostname,
    exRegex = RegExp(protocol + '//' + hostname),
    YQL = 'http' + (/^https/.test (protocol)?'s':'' + '://query.yahooapis.com/v1/public/yql?      callback=?',
    query = 'select * from html where url="{URL}" and xpath="*"';

function isExternal(url) {
    return !exRegex.test(url) && /:\/\//.test(url);
}

return function(o) {

    var url = o.url;

    if ( /get/i.test(o.type) && !/json/i.test(o.dataType) && isExternal(url) ) {

        // Manipulate options so that JSONP-x request is made to YQL

        o.url = YQL;
        o.dataType = 'json';

        o.data = {
            q: query.replace(
                '{URL}',
                url + (o.data ?
                    (/\?/.test(url) ? '&' : '?') + jQuery.param(o.data)
                : '')
            ),
            format: 'xml'
        };

        // Since it's a JSONP request
        // complete === success
        if (!o.success && o.complete) {
            o.success = o.complete;
            delete o.complete;
        }

        o.success = (function(_success){
            return function(data) {

                if (_success) {
                    // Fake XHR callback.
                    _success.call(this, {
                        responseText: data.results[0]
                            // YQL screws with <script>s
                            // Get rid of them
                            .replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')
                    }, 'success');
                }

            };
        })(o.success);

    }

    return _ajax.apply(this, arguments);

 };

})(jQuery.ajax);



$.ajax({
url: your_url,
type: 'GET',
success: function(res) {
    var text = res.responseText;
    // then you can manipulate your text as you wish
    alert(text);
}
});
})(jQuery.ajax);

$.ajax({ url: your_url, type: 'GET', success: 
function(res) { var text = res.responseText; 
// then you can manipulate your text as you wish  
alert(text); } });

【问题讨论】:

  • 可以使用 jQuery 将任何有效的 html 字符串包装在 $() 中,并在其上使用 jQuery 方法。它不必在 DOM 中
  • 我不确定我是否看到你的确切观点,但我认为代码末尾的 res.responseText 是你的外部页面的 html 字符串,你想在它。我说的对吗?
  • 是的,text变量返回外部页面的who html但是我想提取div的

标签: javascript jquery html extract


【解决方案1】:

您可以创建一个 DOM 元素并将变量 text 添加到其中并以这种方式进行操作。

var el = document.createElement('div');
el.innerHTML = text;
document.getElementbyId('MyDiv');

或者使用 jQuery:

var el = $('<div></div>');
el.html(text);
$('#MyDiv', el);

【讨论】:

  • 我无法在 jquery 或 jscript 中使用它:var el = document.createElement('div'); el.innerHTML=文本; el.getElementById('bk62');****************这行不起作用,导致脚本停止运行 alert('test '+ el);
  • 我无法在 jquery 或 javascript 中使用它,这是我正在使用的;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多