如何使用DOM为<a> tag in Javascript?

时间:2017-04-16 00:07:29

标签: javascript

For example, I want to use the DOM to modify: <a href="/helium.html"></a> to equal <a href="/helium.html">Helium</a>

Is there something like the setAttribute() method to do this?

3 个答案:

答案 0 :(得分:0)

是;设置元素的What is the difference between 'git pull' and 'git fetch'?属性。

例如,如果您的<a>标记如下所示:

<a href="/helium.html" id="SOMETHING"></a>

然后更改其标签的代码将是:

document.getElementById("SOMETHING").innerHTML = "Helium";

为了获得更好的表现,正如Derek朕会功夫所建议的那样,请改用innerHTML

document.getElementById("SOMETHING").textContent = "Helium";

答案 1 :(得分:0)

使用querySelector查找具有指定a属性的href元素,在本例中为"/helium.html"

var elems = document.querySelector('a[href="/helium.html"]');
    elems.innerHTML = "Helium";
<a href="/helium.html"></a>

答案 2 :(得分:-1)

假设您的困难是因为该链接上没有ID,这里是JQuery中的一行内容,它将更改与Helium完全链接的文本(这将使用JQuery StartWith 选择器 href 属性):

<script>
        $(document).ready(function () {
        $('a[href="/helium.html"]').text("Helium");
        });
</script>