jQuery .load()页面然后解析html [英] jquery .load() page then parse html

查看:276
本文介绍了jQuery .load()页面然后解析html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用了下面的行.但是现在我需要先解析加载的html,然后再显示它.获取某些html元素的最佳方法是什么.

I have used the line below in my app. But now I need to parse the html loaded before I show it. Whats the best way to get certain html elements.

$("#div").load("page.html");

谢谢

已更新

现在我正在使用它,但是在获取ID为"div"的div的title属性时遇到了麻烦.

Now I am using this but having trouble geting the title attribute of a div with the id "div".

function get_title()
{
    $.get("test.html", function(data) {
        var data = $(data);
        var title = $("#div", data).attr("title");

        alert(title);
    });
}

var数据中的html看起来像这样.

The html in the var data looks like this.

<div id="div" title="title example">
<p>
    Content
</p>
<p>
    Content
</p>
</div>

再次感谢

推荐答案

您可以使用 $.get() ,就像这样:

You can use the semi-equivalent expanded version of $.get(), like this:

$.get("page.html", function(data) {
  var data = $(data);
  //do something
  $("#div").html(data);
});

请注意,在这种情况下,调用 .html(data) 只是

Note that calling .html(data) in this case is just a shortcut for .empty().append(data).

或者,如果可以选择后处理,则只需使用 .load() 回调,例如这个:

Or, if post-processing is an option, just use the .load() callback, like this:

$("#div").load("page.html", function() {
  $(this).find(".class").remove(); //just an example
});

这篇关于jQuery .load()页面然后解析html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆