Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用xpath选择具有onclick的所有锚定标记_Javascript_Html_Xpath - Fatal编程技术网

Javascript 使用xpath选择具有onclick的所有锚定标记

Javascript 使用xpath选择具有onclick的所有锚定标记,javascript,html,xpath,Javascript,Html,Xpath,我想选择一个网页上的所有锚定标记,该网页使用XPATH将“onClick”函数定义为属性。 我的目标网页有如下锚定标记 <a href="status.php?op=del&amp;status_id=2" onclick="return confirm('Are you sure you want to delete this item?')">Delete</a> 我也试过了 //a/@onclick 但这只返回onclick函数定义,正如我所希望的那样,

我想选择一个网页上的所有锚定标记,该网页使用XPATH将“onClick”函数定义为属性。 我的目标网页有如下锚定标记

<a href="status.php?op=del&amp;status_id=2" onclick="return confirm('Are you sure you want to delete this item?')">Delete</a>
我也试过了

//a/@onclick
但这只返回onclick函数定义,正如我所希望的那样,其中包含整个锚标记


问题:如何获取所有使用XPATH将onclick函数定义为属性的锚定标记?

$([onclick])
应该可以找到使用onclick的所有锚定,如果使用jQuery

XPATH
//a[@onclick]
应该可以

尝试在浏览器的控制台中执行此操作,以提醒每个锚:

(function(){
  var withOnclick = document.evaluate('//a[@onclick]', document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
  var anchor = withOnclick.iterateNext();
  while (anchor) {
    alert(anchor.outerHTML);
    anchor = withOnclick.iterateNext();
  }
}())

最后,我能够使用

//a[@onclick]
早些时候,我将此应用于错误的响应对象,因此无法获得链接。 要进一步添加,要获取“url”值,可以执行以下操作:

HtmlXPathSelector(response).select('//a[@onclick]/@href').extract()
这将返回属于锚定标记的所有链接的列表,锚定标记具有定义为属性的“onclick”函数


谢谢你的回答。

//一个[@onclick]
看起来是正确的。
HtmlXPathSelector(response).select('//a[@onclick]/@href').extract()