I have the following structure in my document:
<div id="clientL开发者_开发问答ist>
<a href=whatever.php" att="something">Text</a>
<a href=whatever.php" att="something">Text</a>
</div>
Say I want to get all of the links between the div out, how would I do this?
I tried the following:
$('#clientList').children("a").each(function() {
var x = $(this).html();
});
But I don't get the full link, all I get is the "Text" part in between the links. How can I get the full HTML string of each link, including the href and other attributes?
I believe this is a dupe of JQuery: Select html of an element, inclusive?
Anyway, as mentioned there, you should be able to write an outerHTML plugin with:
jQuery.fn.outerHTML = function() {
return $('<a>').append(this.eq(0).clone()).html();
};
and then use it as
$(this).outerHTML();
If your using jquery 1.4 you can use that unwrap() I have not played much around with it yet but this is what I got so far.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(function()
{
var h = $("#clientList a").unwrap();
jQuery.each(h, function(i, val)
{
alert(val);
});
});
</script>
</head>
<body>
<div id="clientList">
<a href=whatever.php" att="something">Text</a>
<a href=whatever.php" att="something">Text</a>
</div>
</body>
</html>
this will get you the full href path. Right now it does not get the '' attribute right now. Not sure how to do it.
have you tried? :
$('#clientList').children('a').each(function() {
alert($(this)[0].outerHTML);
});
精彩评论