Javascript 如何引用id并获取链接的href?

Javascript 如何引用id并获取链接的href?,javascript,anchor,href,concat,Javascript,Anchor,Href,Concat,问题:如何引用链接的id?请参阅所附图片 如何引用id=视频标题链接 有两个锚定标记*具有相同的类名。我想引用id=视频标题链接 更新:轮询 输出:您可以使用querySelector和.getAttribute执行以下操作: 常量元素=document.querySelector.yt-simple-endpoint const href=element.getAttributeRef; console.loghref 欢迎来到SO。你能给我们更多的信息吗,你到底需要做什么?你必须解释你想

问题:如何引用链接的id?请参阅所附图片

如何引用id=视频标题链接

有两个锚定标记*具有相同的类名。我想引用id=视频标题链接

更新:轮询

输出:

您可以使用querySelector和.getAttribute执行以下操作:

常量元素=document.querySelector.yt-simple-endpoint const href=element.getAttributeRef; console.loghref
欢迎来到SO。你能给我们更多的信息吗,你到底需要做什么?你必须解释你想在这里做什么?你试过什么?您希望在哪个事件上获得href?请将所有代码作为文本包含,而不是作为文本图片包含。还请解释您在该图像中显示的HTML与JavaScript的关系,以及您计划如何使用该JavaScript。如果您只需要链接的href,您可以使用querySelector获取它,然后查看href:document.querySelector'classnamehere'。href。这将为您提供href的完整路径。请注意,除非您正在为Google编写此代码,否则document.location.href不太可能在控制台或扩展中运行代码。
function insertButton(refresh = false) {
var to_match = 'a[class="';
var PAGE_TYPE = 0;

if (document.location.href == "https://www.youtube.com") {
    
var anchor_tag = document.getElementById("video-title-link");
  if(anchor_tag.hasAttribute('href="\/watch\?v=(\S+)"')){

    to_match = to_match.concat("yt-simple-endpoint style-scope ytd-rich-grid-video-renderer", '"]').getattribute('id ="video-title-link"');
    PAGE_TYPE = 1;

  } 

}
const poller = setInterval(function(){  
    
  const anchor_tags = document.querySelectorAll('a[id="video-title-link"]'); 
  
    if(anchor_tags){
  
        for (index = 0; index < anchor_tags.length ; index++){
            
            console.log(anchor_tags[index].getAttribute('href'));
            
        }   
        
        clearInterval(poller);
        
    }   
    
}, 1000);