为什么从函数调用中分配属性innerHTML无效?

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

我需要你的帮助。我想调用一个函数并将结果分配给元素的内部html,但是它不起作用。下面是代码:

function abc() {
  var a = document.getElementById("take").value;

  function Ram() {
    document.write("your name is : Ram <br>");
    document.write("your vill/post : Sunsyari <br>");
    document.write("your block is : Betalghat <br>");
    document.write("your Total land is: 5 bega <br>");
  }

  if (a == "Ram" || a == "ram") {
    document.getElementById("show").innerHTML = Ram();
  } else {
    document.getElementById("show").innerHTML = "wrong credential";
  }
}

请帮助该怎么办?

javascript function dom-events innerhtml
1个回答
1
投票

InnerHTML属性接受一个字符串,但是在您的情况下,您实际上并没有从Ram函数返回任何内容。尝试以下代码段

function abc() {
  var a = document.getElementById("take").value;

  function Ram() {
    return `your name is : Ram <br>
    your vill/post : Sunsyari <br>
    your block is : Betalghat <br>
    your Total land is: 5 bega <br>`
  }
  if (a.toLowerCase()=="ram") {
    document.getElementById("show").innerHTML = Ram();
  } else {
    document.getElementById("show").innerHTML = "wrong credential";
  }
}
abc()
#take{
background:yellow;
}
<input value="ram" id="take">
<div id="show"><div>
© www.soinside.com 2019 - 2024. All rights reserved.