【问题标题】:getting href value of from <a> tag从 <a> 标签获取 href 值
【发布时间】:2010-10-27 14:27:54
【问题描述】:

我有以下html:

<div class="threeimages" id="txtCss">  
<a>   
       <img alt="Australia" src="/Images/Services%20button_tcm7-9688.gif"/>
</a>        
    <div class="text" id="txtLink">            
        <h2>
            <a href="/partnerzone/downloadarea/school-information/australia/index.aspx">Australia</a>
        </h2>            
        <p>Land of the sunshine!</p>        
    </div>
</div>

现在,如果您在 div ID "txtLink" 中看到 href,即 Australia

我希望在页面运行时将相同的 href 值复制到 div ID“txtCss”的上述标签中,我的意思是当我的页面显示时,我的 html 将如下所示:

<div class="threeimages" id="txtCss">  
<a href="/partnerzone/downloadarea/school-information/australia/index.aspx">   
       <img alt="Australia" src="/Images/Services%20button_tcm7-9688.gif"/>
</a>        
    <div class="text" id="txtLink">            
        <h2>
            <a href="/partnerzone/downloadarea/school-information/australia/index.aspx">Australia</a>
        </h2>            
        <p>Land of the sunshine!</p>        
    </div>
</div>

请为上述问题提出一些代码

【问题讨论】:

  • 确保在发布代码时将其设置在代码块中,方法是在每行前面放置 4 个空格,或者突出显示代码并单击“代码”按钮,它带有1 和 0 就可以了。
  • 我认为是javascript
  • 您使用的是哪种编程语言? PHP、ASP.NET、python、纯 HTML...?
  • 如果我用纯 html 得到它会很好

标签: javascript html href


【解决方案1】:

更新
这里没有 jquery 的答案:https://stackoverflow.com/a/887348/11333
早在 2009 年,使用 jquery 是完全可以接受的 :)

创建一个类似这样的 js 文件:

$(document).ready(function() {
    $('.threeimages').each(function(){
    $(this).DupeHref();
    });
});

jQuery.fn.DupeHref =  function(){       
    var target = $(this).find(".text h2 a").attr("href");
    $(this).find("a").attr("href", target);
}

在您的 html 中引用 jquery 和此 javascript 文件。 像这样的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Test</title>         
    </head>
    <body>
        <div class="threeimages">  
            <a>   
                <img alt="Australia" src="/Images/Services%20button_tcm7-9688.gif"/>
            </a>        
            <div class="text">            
                <h2>
                    <a href="/partnerzone/downloadarea/school-information/australia/index.aspx">Australia</a>
                </h2>            
                <p>Land of the sunshine!</p>        
            </div>
        </div>
        <div class="threeimages">  
                     <a>   
                <img alt="Belgium" src="/Images/Services%20button_tcm7-9689.gif"/>
            </a>        
            <div class="text">            
                <h2>
                        <a href="/partnerzone/downloadarea/school-information/belgium/index.aspx">Belgium</a>
                </h2>            
                <p>Land of beer and food!</p>        
            </div>
        </div>
        <script src="./content/js/jquery-1.2.6.min.js" type="text/javascript"></script>
        <script src="./content/js/dupetargets.js" type="text/javascript"></script>
    </body>
</html>

【讨论】:

  • 等一下,我给你写点东西
  • 我在上面的问题中错过的一件事是有三个 div id "txtCss" 和三个 div ID "txtLink",如果我使用你的代码,那么它会在 "$" 上显示错误错误:$ 未定义源文件:cmsstag/js/manoj.js 行:1
  • $ 来自 jquery。确保您正在导入 jquery 库。永远不会有多个节点具有相同的 id。要么你必须给他们唯一的 id,要么你必须删除 id 并使用类。可以更改脚本以接受三图像分类节点并且仅在该节点中工作。
  • 如何将这段代码用于“类”请给我导入jquery的链接。
  • 嗨,鲍里斯,你能不能使用类而不是 ID 为我编写示例代码,以及我需要添加的内容以删除错误“$ 未定义”
【解决方案2】:

这是一个简单易懂的代码:

 <html>
  <head>
   <script language="javascript">  
  function simpleChangeURL(){
        var anchors = document.getElementsByTagName('a');
        if(anchors != null & anchors.length > 0){
            anchors[0].href = anchors[1].href;  
        }   
    }

    function simpleChangeByAustraliaURL()
    {
        var anchors = document.getElementsByTagName('a');
        var images = document.getElementsByTagName('img');
        var imageNeeded;
        var anchorNeeded;
        if(images != null){
            for( var i = 0; i < images.length ; i++){
                if (images[i].alt == 'Australia') imageNeeded = images[i];
            }
        }
        if(anchors != null){
            for( var j = 0; j < anchors.length ; j++){
                if (anchors[j].firstChild.data == 'Australia') anchorNeeded = anchors[j];
            }
        }
        if(imageNeeded != null && anchorNeeded!= null){
    var imageAnchor = imageNeeded.parentNode;
    imageAnchor.href = anchorNeeded;

}

    }


    </script>
    </head>

    <body>
    <div class="threeimages" id="txtCss">  
    <a>   
           <img alt="Australia" src="/Images/Services%20button_tcm7-9688.gif"/>
    </a>        
        <div class="text" id="txtLink" >            
            <h2>
                    <a href="/partnerzone/downloadarea/school-information/australia/index.aspx">Australia</a>
            </h2>            
            <p>Land of the sunshine!</p>        
        </div>
    </div>
    <script> simpleChangeByAustraliaURL();</script>
    </body>
    </html>

【讨论】:

    【解决方案3】:

    这是不使用任何库的最短答案,只适用于你想要的东西

    var tc = document.getElementById("txtCss");
    var ary = tc ? tc.getElementsByTagName("a") : [];
    if(ary.length >= 2)
        ary[0].href = ary[1].href;
    

    【讨论】:

    • 久违了,值得分享一个:)Array.from(document.querySelectorAll("#textCss a")).slice(0, 2).forEach((i, t, ary) =&gt; i === 1 &amp;&amp; (ary[0].href = t.href))
    【解决方案4】:

    你可以使用:

    var txtLink = document.getElementById('txtLink');
    var a = txtLink.getElementsByTagName('a');
    if (a != null && a.length > 0) {
        var setLink = txtLink.parentNode.getElementsByTagName('a');
        if (setLink != null && setLink.length > 0) {
            setLink[0].href = a[0].href;
        }
    }    
    

    我认为这应该可行..

    【讨论】:

      猜你喜欢
      • 2013-03-04
      • 2021-12-26
      • 2018-05-14
      • 2017-06-04
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多