问题描述
我使用其他人的应用程序,并希望在任何< p>之间更改innerHTML。一>< / a>标签具有一定的href。但是这些链接没有与它们关联的类或ID,我不能编辑代码以给它们类或ID。有没有办法通过JavaScript中的href获取标签?我想做类似这样的事情:
var theLink = document.getElementByHref(example.com);
否则,如果这不可行,我可以循环浏览页面中的所有链接并选择那些具有特定href和innerHTML的数据库我正在寻找?
您可以使用DOM3属性选择器( jQuery doc )来获取所有包含 href
属性。它看起来就像
$('a [href * =example.com]')
code>
但是,这可能不是您真正想要的 - 不仅可能包含此字符串的网址。你可能会做一些类似于开始 - :
$ $ $ $'''[href ^ =http://example.com ]')
但是为了得到一个精确而可能更复杂的匹配,自定义:
$('a [href]')。filter(function(){
return this.hostname ==example.com;
//检查锚点的其他属性元素
})
I'm using someone else's app and want to change the innerHTML in between any < a>< /a> tag that has a certain href. But these links don't have a class or ID associated with them and I can't edit the code to give them classes or ID's. Is there a way to grab a tag by its href in JavaScript? I wanted to do something similar to this:
var theLink = document.getElementByHref("example.com");
Otherwise, if that is not possible, can I loop through all the links in the page and choose the ones that have the certain href and innerHTML I'm looking for?
You can use a DOM3-attribute-selector (jQuery doc) to get all elements that contain a certain text in their href
attribute. It would look like
$('a[href*="example.com"]')
However, that might not be what you actually want - not only urls to that domain might contain this string. You might do something like begins-with:
$('a[href^="http://example.com"]')
but to get an exact and possibly more complex match, you don't get around a custom filter:
$('a[href]').filter( function() {
return this.hostname == "example.com";
// or check other properties of the anchor element
})
这篇关于如果它没有类或ID,是否有可能通过它的href获取链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!