GetElementyID("ID").innerHTML = 值无法按预期工作

问题描述 投票:0回答:1

我尝试更改顶部导航栏中 HTML 中的文本值和字符串颜色(img 资源已删除)。

这里有什么错误吗?如果 ELement 嵌套在类中,引用 ELement 的 ID 是否足够?目前这种方法不起作用(HTML 没有变化)。

可以通过导航栏(语言选择)将 HTML 页面上的文本值替换为自定义/翻译文本进行快速测试。

故障描述:
单击按钮元素时,文本元素不会更改。

<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<head></head>
<style>

.topnav {
  overflow: hidden;
  width: 645px;
  background-color: white;
  border-top: 1px solid black;
  border-bottom: 1px solid black;
  border-right: 1px solid black;
  position: center;
  margin-left: -32px;
}

.topnav a {
  float: left;
  color: black;
  background-color: white;
  text-align: center;
  padding: 10px 8px;
  text-decoration: none;
  text-align: center;
  font-weight: 600;
  font-size: 16px;
  border-left: 1px solid black;
}

.topnav a:hover {
  background-color: darkgrey;
  color: white;
  cursor: pointer;
}

div a img {
    width: 21px;
    height: 12px;
    margin-left: 5px;
}

</style>
<body>
    
<script>

    function funcAT(){
        document.getElementById("TEST1").innerHTML ="TestOfFunction";
        document.getElementById("TEST1").style.color = "red";
    }
    
    function funcDE(){
        document.getElementById("TEST2").innerHTML ="TestOfFunction 2";
        document.getElementById("TEST2").style.color = "red";
    }
    
        
</script>

<div class="topnav">
    
  <a class="lang_btn"><i class="fa fa-commenting-o"></i>&nbsp; Verfügbare Sprachen und Länder</a>
        <a><img id="AT" onclick="funcAT()" src="...">&nbsp;&nbsp;AT</a>
        <a><img id="DE" onclick="funcDE()"  src="...">&nbsp;&nbsp;DE</a>
        <a><img id="PL" onclick="funcPL()"  src="...">&nbsp;&nbsp;PL</a>
        <a><img id="RO" onclick="funcRO()"  src="...">&nbsp;&nbsp;RO</a>
        <a><img id="UK" onclick="funcUK()"  src="...">&nbsp;&nbsp;UK</a>
</div><br>

<p id="TEST" >predefined value</p>
<p id="TEST1">predefined value 1</p>
<p id="TEST2">predefined value 2</p>

</body>
</html>
html css onclicklistener
1个回答
0
投票

解决方案:
顶部导航栏元素使用按钮而不是img+文本,因为实际方法只有直接点击img元素才会出现效果。

不幸的是,我没有注意到这一点,因为我将整个元素解释为功能上的按钮(但没有以这种方式实现)。

感谢您的帮助!

© www.soinside.com 2019 - 2024. All rights reserved.