javascript - 我如何在 $.get(url) 的项目上使用 jquery

标签 javascript jquery html

我想获取一个页面,然后使用 jquery 查找链接等元素。如何让 $('#blah') 搜索获取数据而不是页面?

最佳答案

您应该能够从返回的 HTML 创建一个 dom 元素,而无需将其实际添加到文档中,然后使用 jQuery 方法进行搜索:

jQuery.get('/my_url/', function(html_data) {
  // If your html_data isn't already wrapped with an HTML object, you may
  // need to wrap it like so:
  //
  // var jQueryObject = $("<div>" + html_data + "</div>");
  var jQueryObject = $(html_data);
  jQueryObject.find("a.link_class");

  // Or, as stated by gregmac below, you could just do the following:
  $("a.link_class", html_data);

  // or, if wrapping is required:
  $("a.link_class", "<div>" + html_data + "<div>");
});

关于javascript - 我如何在 $.get(url) 的项目上使用 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2509659/

相关文章:

javascript - javascript中带标签和不带标签的区别是什么

javascript - Morris Js 和 Codeigniter 从 Controller 传递数据

jQuery - 模拟背景大小的封面

javascript - 为什么 <a href ='javascript:"text";'> 不起作用?

java - 在调用 Servlet 之前运行 Javascript 函数

javascript - 如何在 JS 中判断某个函数/方法是否存在于 IE7 中?

javascript - 通过具有不同键的对象数组进行映射

javascript - 如何在测试值或子对象之前测试父对象的存在

jquery - 尝试对 iframe 进行跨脚本编写时,如何禁用访问被拒绝消息?

html - div中的图像不根据div百分比调整大小