首页 > 解决方案 > 在javascript中的.textContent中添加链接?

问题描述

<button type="button" onclick="myFunction()">日本語&lt;/button>
<script>
function myFunction() {
document.getElementsByClassName("childdiv")[0].childNodes[1].textContent="私について.link("Aboutme.html") | ギャラリー.link("Gallery.html") | 連絡.link("contact.html")";
}
</script>
<div class="childdiv">
<h3 id="english">&nbsp;<a href="Aboutme.html">About me</a> | <a href="Gallery.html">Gallery</a> | <a href="contact.html">Contact</a></h3>

这是我目前使用的代码。普通文本完美运行,但在 .textContent 中使用超链接时,按钮甚至不起作用。有谁知道解决方案?

标签: javascripttexthyperlink

解决方案


编辑:下面的新代码将文本和按钮更新为日语 onclick。

//we are targeting the current links by id
var a = document.getElementById('link1');
var b = document.getElementById('link2');
var c = document.getElementById('link3');

//this is for example text 
var p = document.getElementById('example');

//this function will take English links/text and update to Japanese when button is clicked
function myFunction() {
p.innerHTML = "Links and text are now Japanese"
if (a) {
  a.innerHTML = "私について";
  a.href = "私について.link";
}
if (b) {
  b.innerHTML = "私について";
  b.href = "私について.link";
}
if (c) {
  c.innerHTML = "連絡";
  c.href = "連絡.link";
}
}
<button id="japanese" type="button" onclick="myFunction();">日本語&lt;/button>

<div id="childdiv">
<h3 id="english">&nbsp;<a id="link1" href="Aboutme.html">About me</a> | <a id="link2" href="Gallery.html">Gallery</a> | <a id="link3" href="contact.html">Contact</a></h3>
</div>

<p id="example">Click Button to update links and text to Japanese</p>


推荐阅读