javascript - 使用 AJAX/jQuery 查找 THIS div 或 THAT div

标签 javascript jquery ajax get

您可以通过 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');

Documentation :

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/

相关文章:

javascript - Vue.js 向组件添加函数

php - Jquery 使用 $.post 函数进行实时输入验证

javascript - 如何定期更新Ember的模型(例如在setInterval中)?

javascript - 如何检查用户是否已登录 javascript

jquery - 尖边的制作方法

javascript - 主干刷新 View 事件

asp.net-mvc - 使用 jQuery + ASP.NET MVC 自动保存表单输入

javascript - 如何防止那些 PHP 变量被缓存在 WordPress 上?

javascript - 在客户端显示服务器返回的HTML页面

javascript - 在具有多个 url 的 javascript 中添加 JSON