[英]How do you use Xpath to find All anchor tags “In General” within a div Inner html
这个问题饲料的关闭这一个。
我只想在字符串中找到任何锚标记。 以下示例类似于上述问题。 除了我只想使用 xpath 检测锚标记。 最好有角度。 使用 ViewChild。 接受任何使用 javascript 的解决方案。
// Our HTML source
var s = `Some Html String <a href="#">something1</a>`;
// Create a root div because XML requires a single root element
var div = document.createElement('div');
// Set the innerHTML to our string
div.innerHTML = s;
// Find any <a> tags
var iterator = document.evaluate('//a]', div, null, XPathResult.ANY_TYPE, null);
// Set the iterator
var thisNode = iterator.iterateNext();
// Loop the iterator and log the node found
while (thisNode) {
console.log(thisNode);
thisNode = iterator.iterateNext();
}
这样对吗?
这是你的答案。
const desc = this.description.value;
const div = document.createElement('div');
div.innerHTML = desc;
const iterator = document.evaluate('//a', div, null, XPathResult.ANY_TYPE, null);
const anchors = iterator.iterateNext();
while (anchors) {
this.toastr.error("To better improve service, external links are not permitted.");
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.