简体   繁体   English

使用JavaScript更改锚文本

[英]Changing anchor text with JavaScript

Example HTML: 示例HTML:

<div id="video-quality" style="">
<a class="low bttn" href="">
<span>Low Quality</span>
</a>
</div>

Hey im trying to use JavaScript to target an anchor tag and change it's current text of "Low Quality" to "High Quality". 嘿我试图使用JavaScript来定位锚标签并将其当前的“低质量”文本更改为“高质量”。

I don't really know JavaScript very well, but this is what I have come up with. 我不太了解JavaScript,但这是我想出来的。 But it's obviously not working: 但它显然不起作用:

var vid_id=document.getElementById('video-quality');
var anchor=vid_id.getElementsByTagName('a');
anchor.innerHTML="High Quality";

Assuming the video-quality div will only ever have one anchor in it, you're not far off: 假设video-quality div只有一个锚点,你就不远了:

var vid_id=document.getElementById('video-quality');
var anchor=vid_id.getElementsByTagName('a');
anchor[0].innerHTML="High Quality";
//    ^^^-- change here

(There's a shorter way if you don't need to support IE7 and earlier, see David's answer for more.) (如果您不需要支持IE7及更早版本,可以采用更短的方式,请参阅David的答案 。)

The key was in the name getElementsByTagName (note the plural). 关键是名称getElementsByTagName (注意复数)。 It returns a NodeList , not an element. 它返回一个NodeList ,而不是一个元素。 A NodeList is (shockingly!) a list of matching nodes, in this case matching elements . NodeList (令人震惊!)是匹配节点的列表,在这种情况下是匹配元素 innerHTML is a property of individual elements, so we have to index into the NodeList to access a specific element. innerHTML是各个元素的属性,因此我们必须索引到NodeList以访问特定元素。 The code above assumes there is at least one matching element, and updates its HTML. 上面的代码假设至少有一个匹配元素,并更新其HTML。 If you want to be more defensive: 如果你想要更加防守:

var vid_id=document.getElementById('video-quality');
var anchors=vid_id.getElementsByTagName('a');
if (anchors[0]) {
    anchors[0].innerHTML="High Quality";
}

That allows for the possibility there are no matching anchors. 这允许没有匹配的锚点的可能性。

Note that by doing this, you're removing the span element, since the span is a descendant of the anchor. 请注意,通过执行此操作,您将删除span元素,因为span是锚的后代。 If you wanted to avoid that, just look for span elements as descendants of the anchor, exactly as you've looked for anchors in the div . 如果你想避免这种情况,只需将span元素作为锚的后代,就像你在div寻找锚一样。


FWIW, I'd recommend using a reasonable JavaScript browser-focussed library like jQuery , YUI , Closure , or any of several others . FWIW,我建议使用一个合理的JavaScript浏览器聚焦库,如jQueryYUIClosure其他几个 They provide a lot of utility functionality, and smooth over some browser differences. 它们提供了许多实用功能,并且可以平滑一些浏览器差异。 But if you prefer using the DOM directly, that's fine too. 但如果您更喜欢直接使用DOM,那也没关系。

If you don't need legacy support (IE7-), you can write: 如果您不需要传统支持(IE7-),您可以写:

document.querySelector('#video-quality a span').innerHTML = 'High Quality';

If you DO need legacy support, I recommend a library like jQuery or similar to make selecting DOM nodes much simpler from a singe, wide-spread API. 如果您需要遗留支持,我建议使用类似jQuery或类似的库,以便从一个广泛的API中选择更简单的DOM节点。

Plain javascript: 简单的javascript:

var vid_id = document.getElementById('video-quality');
var span = vid_id.getElementsByTagName('span');
span[0].innerText='High Quality'

Or you can just use jQuery: 或者你可以只使用jQuery:

$('#video-quality span').text('High Quality');

hope this helps. 希望这可以帮助。

Simple is best. 简单是最好的。 Just give the span element an id and then alter it directly. 只需给span元素一个id,然后直接改变它。

<div id="video-quality" style="">
<a class="low bttn" href="">
<span id="quality-setting">Low Quality</span>
</a>
</div>

then script is: 然后脚本是:

document.getElementById('quality-setting').innerHTML="High Quality";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM