如何基于javascript函数中的值呈现Div内容

如何基于javascript函数中的值呈现Div内容,javascript,html,asp.net-core,conditional-rendering,Javascript,Html,Asp.net Core,Conditional Rendering,我有一个场景,div的内容将根据java脚本函数返回的值显示 myFunction() { var x = false; // Some code to decide value of x. return x; } 伪HTML: <div> if(true==myfunction()) { <p>True is returned from the javascript function</p> } else { <p>False is ret

我有一个场景,
div
的内容将根据java脚本函数返回的值显示

myFunction()
{
var x = false;

// Some code to decide value of x.

return x;
}
伪HTML:

<div>
if(true==myfunction())
{
<p>True is returned from the javascript function</p>
}
else
{
<p>False is returned from the javascript function</p>
}
</div>
我使用的是Asp.net内核,我的JavaScript代码出现在site.js中,html代码出现在cshtml中

如果有人能就如何实现这一目标提出建议,那将非常有帮助。
注意:提供的代码不是实际代码,而是实际场景。

这可以是一种方法

<代码> const PAR=文档.CuraEngEnter(“p”); PAR.NEnHTML=‘${x’‘true’:“false”}是从JavaScript函数中呈现的; document.getElementById('idx').appendChild(par); 你需要给你的div标签一个id
html

有很多不同的方法可以做到这一点。这实际上取决于您的代码以及何时以及如何设置这些值

下面的简单示例是通过单击按钮来实现的。您的问题中没有足够的信息,无法知道这是否是您的最佳选择

建议用真实的代码更新你的问题,这样你就能得到一个更接近你真正需要的答案。请包括您收到的任何错误消息

函数myFunction(){
var spn=document.getElementById(“jsValue”);
var val=(this.value==“1”)?“True”:“False”;
spn.textContent=val;
}
window.onload=函数(){
var b=document.querySelectorAll(“按钮”);
变量i,最大值=b.长度;

对于(i=0;i您可以创建一个新的DOM节点,然后将其附加到带有条件的主体中或其他任何内容

<div id="div1"></div>

<script>
  function myFunction(x) {
    var para = document.createElement("p");
    if (x === true) {
      var node = document.createTextNode(
        "True is returned from the javascript function."
      );
      para.appendChild(node);
    } else {
      var node = document.createTextNode(
        "False is returned from the javascript function."
      );
      para.appendChild(node);
    }
    var element = document.getElementById("div1");
    element.appendChild(para);
  }

  myFunction(true);
</script>

函数myFunction(x){
var para=document.createElement(“p”);
如果(x==true){
var节点=document.createTextNode(
“从javascript函数返回True。”
);
子节点(节点)段;
}否则{
var节点=document.createTextNode(
“javascript函数返回False。”
);
子节点(节点)段;
}
var元素=document.getElementById(“div1”);
要素.附件(第6段);
}
myFunction(true);

更多信息:

您使用的是razor视图吗?这正是我一直在寻找的解决方案。非常感谢您的及时回复。@RishabhNigam很高兴我能提供帮助。您可以选择接受此答案;)