簡體   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