您可以通过 AJAX 加载跨域 div(使用 this plug-in ),如下所示:
$.ajax({
url: 'http://externalurl.com',
type: 'GET',
success: function(res) {
var artwork = $(res.responseText).find('.div1 img');
$('.mydiv').append(artwork);
}
});
这将返回 div1
中的 img 标签。但是是否可以使用 2 个单独的 div
在源网站上搜索内容?
例如(显然是伪代码):
var artwork = $(res.responseText).find('.div1 img') or .find('.div2 img');
知道 div1 和 div2 永远不会位于源站点的同一页面上。源站点将仅具有 div1 或 div 2,但绝不会同时具有两者。
最佳答案
是的,一旦你调用 $(res.responseText)
jQuery 就会将其解析为 DOM 树。
然后您可以使用常规 jQuery 选择器(包括复合选择器)来“查找”正确的节点。
$(res.responseText).find('.div1 img,.div2 img');
Multiple Selector (“selector1, selector2, selectorN”)
Description: Selects the combined results of all the specified selectors. (version added: 1.0j)
Query( "selector1, selector2, selectorN" )
- selector1: Any valid selector.
- selector2: Another valid selector.
- selectorN: As many more valid selectors as you like.
You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order. An alternative to this combinator is the
.add()
method.
关于javascript - 使用 AJAX/jQuery 查找 THIS div 或 THAT div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33244615/