查看: 490|回复: 0

[文章教程] JavaScript By xPath获取元素对象

[复制链接]
xuanxiao 发表于 2022-6-21 17:04:01 | 显示全部楼层 |阅读模式
For ID

  1. //div[contains(@id,"content-body")]//p #to Select all Paragraphs

  2. //div[contains(@id,"content-body")]//p//text() # To Select all text in Paragraphs
复制代码



For Class

  1. //*[contains(@class,"content-body")//p
  2. //div[contains(@class,"content-body")]//p//text()
复制代码


For class in class

  1. //*[contains(@class,"content-body") and contains(@class,"another-sub-content-body")]//p//text()
复制代码


  1. //get text
  2. var iTotal=getElementByXpath("//td[contains(@id,objID)]//a//div[1]").innerText
  3. var sName=getElementByXpath("//td[contains(@id,objID)]//a//div[2]").innerText
  4. //console.log(iTotal,sName);

  5. $("#iTotal").html(iTotal);
  6. $("#sName").html(sName);

  7. function getElementByXpath(path) {
  8.   return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  9. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表

在线客服

售前咨询
售后咨询
服务热线
023-58418553
微信公众号