使用jQuery遍历HTML

Traversing through HTML with jQuery

本文关键字:HTML 遍历 jQuery 使用      更新时间:2023-09-26

我使用的是CasperJS,我已经检索到了要遍历的HTML表,但很难理解如何获取这个特定块中每个元素的值。

这就是我现在拥有的:

var baseElement = document.createElement("div");
var jQueryElement = jQuery(baseElement);
jQuery(jQueryElement ).prepend(menuItemObj.sections);  
var magicElement = jQuery(jQueryElement)[0];

menuItemObj是通过this.evaluate 中的map.call收集的HTML节点数组

现在,我正试图通过查找特定的h6标签值来提取内容。

如果我这样做:

jQuery(".prdDe h6", magicElement).text() 

我得到了所有节点组合在一起的文本,即Coca-ColaDiet Coca-Cola SpriteWaterCrush Orange Ginger AleIced Tea

我一辈子都想不出如何将它们分离成一个数组,

我试过这个:

var titles = jQuery(".prdDe h6", magicElement);
for (var t in titles)
   console.log(titles[t].text())

但它不喜欢这样。。。

有人知道如何单独挑选元素的价值吗?

非常感谢您的帮助。

谢谢!

var titles = jQuery(".prdDe h6", magicElement).toArray();是如何将jquery集合转换为数组的。

根据循环时要做的操作,您可能需要使用jquery的.ech()。