javascript - 在javascript中的.textContent中添加链接?
问题描述
<button type="button" onclick="myFunction()">日本語</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"> <a href="Aboutme.html">About me</a> | <a href="Gallery.html">Gallery</a> | <a href="contact.html">Contact</a></h3>
这是我目前使用的代码。普通文本完美运行,但在 .textContent 中使用超链接时,按钮甚至不起作用。有谁知道解决方案?
解决方案
编辑:下面的新代码将文本和按钮更新为日语 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();">日本語</button>
<div id="childdiv">
<h3 id="english"> <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>
推荐阅读
- apache-kafka - Kafka Streams / 减少和日志保留
- excel - 在excel中的一致时间轴上绘制随机时间
- python - 删除空格并保留 \n \t .. 等
- c# - 如何在 c# 中清除 System.Azure.EventHubs.EventDataBatch 中的值?
- c# - 如何使 nhibernate 不结合继承对象的结果?
- c++ - 从非模板基类扩展的模板类和覆盖函数参数
- spring-mvc - Kubernetes 中微服务之间的身份验证
- google-chrome - Chrome 操作系统上的 RAM 消失了
- javascript - Mongoose Schema,存储一个可能有也可能没有属性的对象
- reactjs - 如何识别路由是通过history.push还是手动地址更改(react-router)启动的