繁体   English   中英

(javascript) 将函数的变量放在 p 标签中

[英](javascript) put the variable of function in p tag

我想将函数的结果放在p标签中。

但我收到这样的消息

“syntaxError: 意外的标识符($(#vol1).html(result);)

有什么问题?

当我在函数中执行alert(result)时,它运行良好。

<p id ="vol1"></p>

<script type="text/javascript">
function getResult(){
    var con1 = parseInt($('#concentration1').val());
    var vol = parseInt($('#volume').val());
    var con2 = parseInt($('#concentration2').val());
    var result = Math.round((con2*vol)/con1);
    $(#vol1).html(result);
}
</script>

如果您可以检查您的代码,#vol1 应该用引号括起来,即 $('#vol1')。 其他一切都很完美。

 function getResult() { var con1 = parseInt($('#concentration1').val()); var vol = parseInt($('#volume').val()); var con2 = parseInt($('#concentration2').val()); var result = Math.round((con2 * vol) / con1); alert(result ? result : "not found"); // if result $('#vol1').html(result ? result : "not found"); } getResult();
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p id="vol1">12</p>

#vol1缺少引号,必须是'#vol1'

你可以试试这个吗

document.getElementById('vol1').innerHTML = result;

或者你可以这样做(通过 JQuery):

$('#vol1').html(result);

你可以试试这个: $('#vol1')

或更改为: $('#vol1').append(result)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM