【发布时间】:2022-01-19 07:35:09
【问题描述】:
我的目标是在鼠标悬停时将文本从“hello”(没有链接)更改为“Google”,并在生成的“Google”文本上提供一个“href”,然后在没有链接的情况下恢复为“hello”onmouseout。
下面的代码可以将文本从“hello”更改为“Google”,但是,
-
“Google”上的链接不起作用(即使我可以右键单击“Google”并在另一个选项卡上打开链接)
-
鼠标退出时文本不会变回“hello”。
提前感谢您的帮助!
这是我的代码:
<style>
.container {
margin-top: 6vw;
margin-left: 40%;
margin-right: 40%;
}
</style>
<div class="container">
<h1>
<div class="hello" id="hello1" onmouseover="changeText()" onmouseout="changeText(this,'Hello.')">Hello.</div>
</h1>
</div>
<script>
function changeText() {
if (document.getElementById("hello1")) {
a = document.getElementById("hello1")
a.innerHTML = '<a href="https://www.google.com">Google</a>'
}
}
</script>
【问题讨论】:
标签: javascript html href onmouseover onmouseout