假设我有这个 html 标记:
<div id="wrapper">
<pre class="highlight">
$(function(){
// hide all links except for the first
$('ul.child:not(:first)').hide();
$("a.slide:first").css("background-color","#FF9900");
$('ul.parent a.slide').click(function(){
$('ul.parent a.slide').css("background-color","#3399FF");
$('ul.parent a.slide').mouseout(function(){
$(this).css("background-color","#3399FF");
});
$('ul.parent a.slide').mouseover(function(){
$(this).css("background-color","#66CC66");
});
});
});
</pre>
</div>
阅读 div 中的每一行代码的最简单方法是什么。如何从该 div 中提取每一行代码或以我想要的任何方式遍历每一行样式。
编辑:
我在包装类之后添加了 pre
标签,请考虑一下。
最佳答案
你的意思是这样的:
var lines = $("#wrapper pre").text().split("\n");
$.each(lines, function(n, elem) {
console.log(elem);
});
因为这是文本(而不是 html),所以您应该这样对待它。唯一的方法是阅读它,然后使用字符串解析例程。
关于javascript - 读取 jQuery 中元素的每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2593046/