我想从我的第二个站点(子域)获取 html 并使用它来构建此站点的一部分。
HTML 网站:
<head>
<body>
<div id = "items">
<div id = "#one">
<ul>
<li><a href = "#">Click here</a></li>
<li><img src = "/images.png" /></li>
</ul>
</div>
<div id = "#two">
<ul>
<li><a href = "#">Click here</a></li>
<li><img src = "/images2.png" /></li>
</ul>
</div>
</div>
</body>
</head>
我想做一些事情,比如计算 div 元素的数量、获取列表等。
这是我尝试过的:
$.ajax({
url: '/mysite/items.html',
success: function(data) {
//Need to manipulate here
var html = $(data);
var items = $.get(".items", html);
var numItems = $('.items').length;
console.log(data); //There is data(Whole HTML is here.)
console.log(items); //This is undefined??
console.log(numItems): //This is undefined??
var raw = html.get(".offers"); //Complains doesnot have a get method??
},
});
最佳答案
从我可以看出你想要什么操作系统
$.ajax({
url: '/mysite/items.html',
success: function (data) {
//Need to manipulate here
var $html = $(data);
//use .find() to find elements with class items in data
var $items = $html.find('.items');
//use the items object to get the length
var numItems = $items.length;
console.log(data);
console.log($items);
console.log(numItems):
//need to use .find() to get the elements matching a selector
var raw = $htm.find(".offers");
},
});
关于javascript - 使用 jquery 抓取 HTML 并操作元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21542772/