gpt4 book ai didi

javascript - 如何从 jQuery 对象获取特定的 html

转载 作者:太空宇宙 更新时间:2023-11-03 23:12:45 25 4
gpt4 key购买 nike

我正在从网站获取 html。我想从该页面获取特定的 html,而不是全部如何获取?我试过跟随;

在将数据附加到下面的目标之前

container.html(data);

我想做 data.find('.site-header').html(); 然后做 container.html(data);

我怎样才能做到这一点?

DEMO

HTML

<div id="target"></div>

脚本

$(function () {
var container = $('#target');
var url = 'http://aamirshahzad.net';

$.getJSON("http://query.yahooapis.com/v1/public/yql?" +
"q=select%20*%20from%20html%20where%20url%3D%22" + encodeURIComponent(url) +
"%22&format=xml'&callback=?",

function (data) {
if (data.results[0]) {
var data = filterData(data.results[0]);
container.html(data);
} else {
var errormsg = '<p>Error: could not load the page.</p>';
container.html(errormsg).focus().effect('highlight', {
color: '#c00'
}, 1000);
}
});

});


function filterData(data) {
// filter all the nasties out
// no body tags
data = data.replace(/<?\/body[^>]*>/g, '');
// no linebreaks
data = data.replace(/[\r|\n]+/g, '');
// no comments
data = data.replace(/<--[\S\s]*?-->/g, '');
// no noscript blocks
data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g, '');
// no script blocks
data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g, '');
// no self closing scripts
data = data.replace(/<script.*\/>/, '');
// [... add as needed ...]
return data;
}

最佳答案

很简单;

container.html($(data).find('.site-header'));

关于javascript - 如何从 jQuery 对象获取特定的 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31991372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com