日期:2014-05-16 浏览次数:20571 次
<div>
T<span id='1' class='red'><span id='2' class='bold'>his</span></span> is <span id='3' class='blue'>som</span>e sample text.
</div>
$('span').each(function(){
textArry.push($(this).text());
classArry.push($(this).attr('class'));
})
<div>
T<span id='1' class='red'><span id='2' class='bold'>his</span></span> is <span id='3' class='blue'>som</span>e sample <span>text</span>.
</div>
<script type="text/javascript" src="http://www.coding123.net/js/jquery.js"></script>
<script>
$(function () {
var cls = {};
$('span').not(':empty').each(function () {
if (this.firstChild.nodeType == 1) return true;
kw = this.innerHTML;
cls[kw] = $(this).parentsUntil('div').add(this).map(function () { return this.className }).get();
});
for (kw in cls) alert(kw + '=' + cls[kw]);
});
</script>
$(function(){
var data = {} , key;
$('span').each(function(){
key = $.trim($(this).text());
if(!data.hasOwnProperty(key)){
data[key] = [];
}
data[key].push($(this).attr('class'));
});
console.log(data);
});