简体   繁体   English

如何在子元素的页面加载时替换innerHTML?

[英]How do I replace innerHTML on page load of a child element?

Let's start with I'm not fluent in javascript, but I know a little.让我们从我不精通 javascript 开始,但我知道一点。 I'm on a platform that is running code I can't see on the back-end.我在一个运行我在后端看不到的代码的平台上。 That means I can only control so much.这意味着我只能控制这么多。 For example, there is a tag:例如,有一个标签:

{{ component.quality_switch }}

When the page loads, it swaps that with:当页面加载时,它与以下内容交换:

<div class="co-quality-switch" data-behavior="quality_switch">
    <a class="co-quality-switch" href="#" data-behavior="switch-low" style="">
        <i class="co-icon-video-camera"></i>
        watch in high quality
    </a>
</div>

My goal, is to replace the text of the "a" link from "watch in high quality" to "toggle captions".我的目标是将“a”链接的文本从“高质量观看”替换为“切换字幕”。 So I experimented and removed the first {{component.quality_switch}} tag and replaced it with the outer div it normally pulls, with an ID added to the div:所以我尝试并删除了第一个 {{component.quality_switch}} 标签,并用它通常拉的外部 div 替换它,并在 div 中添加了一个 ID:

<div id="captiontoggle" class="co-quality-switch" data-behavior="quality_switch">
</div>

When the page loaded, it spat back the same as before, with my added id on the div.当页面加载时,它会像以前一样吐出来,我在 div 上添加了 id。 Progress!进步!

<div id="captiontoggle" class="co-quality-switch" data-behavior="quality_switch">
    <a class="co-quality-switch" href="#" data-behavior="switch-low" style="">
        <i class="co-icon-video-camera"></i>
        watch in high quality
    </a>
</div>

So apparently there is some java on the back-end on the data-behavior="quality_switch" part that is adding all this code.因此,显然在后端的data-behavior="quality_switch"部分有一些 java 正在添加所有这些代码。 The kicker is, I need all the other things that come with that data-behavior command as that is what swaps my video source to the caption version and back when clicked.关键是,我需要该数据行为命令附带的所有其他内容,因为这会将我的视频源交换到字幕版本并在单击时返回。 So I can't remove it.所以我无法删除它。 So my next thought was let's just use JS to change the innerHTML of that "a" link.所以我的下一个想法是让我们使用 JS 来更改“a”链接的 innerHTML。 The innerHTML is:内部HTML是:

<i class="co-icon-video-camera"></i>watch in low quality

So if I can just swap that to "toggle captions" I'd be all set.所以如果我能把它换成“切换字幕”,我就准备好了。 But how can I swap the innerHTML of the "a" link when it doesn't have an ID?但是,当“a”链接没有 ID 时,如何交换其内部 HTML? I can only assign an ID to it's parent DIV (captiontoggle).我只能为它的父 DIV 分配一个 ID(captiontoggle)。 I tried adding this script:我尝试添加这个脚本:

<script>
var x = document.getElementById("captiontoggle");
var y = x.getElementsByTagName("a");
y.innerHTML = "test";
</script>

... but it didn't change anything. ......但它没有改变任何东西。 From my experimenting it looks like maybe I can't do this 2 layers down... Like I could change the innerHTML of captiontoggle, but not it's "a" element.从我的实验来看,似乎我不能做这 2 层下来......就像我可以改变 captiontoggle 的innerHTML,但不是它的“a”元素。

I'm open to suggestions!我愿意接受建议! Thanks.谢谢。

You need to use parent reference to get children and change there innerHTML您需要使用父引用来获取子项并在那里更改innerHTML

<script> 
  var x = document.getElementById("captiontoggle"); 
  var y = x.children[0]; 
  y.innerHTML = "test"; 
</script>

Working snippet:工作片段:

 var x = document.getElementById("captiontoggle"); var y = x.children[0]; y.innerHTML = "test";
 <div id="captiontoggle" class="co-quality-switch" data-behavior="quality_switch"> <a class="co-quality-switch" href="#" data-behavior="switch-low" style=""> <i class="co-icon-video-camera"></i> watch in high quality </a> </div>

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

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