[英]Onload Javascript/jQuery Not Working In HTML
HTML中的Java腳本如下。
$(document).ready(function () { var found = {{.found}} window.alert("hiiii"); if (foundRecords==true) { document.getElementById("abc").style.display = "block"; } return });
這應該在html加載期間加載。 但這不是全部加載。 在這種簡單的代碼和平中,我沒有發現任何錯誤。
如果您想獲取帶有{{.found}}類的元素
window.onload = function()
{
var found = document.getElementsByClassName("found");
if (found) {
document.getElementById("abc").style.display = "block";
}
}
如果您使用jQuery加載該函數,則將稍作更改:
$(document).ready(function () {
// get the class found and assign it to a variable found
var found = $('.found') // it was {{.found}} producing an error
window.alert("hiiii");
// where does foundRecords come from? it is up to you to clear this
if ( foundRecords == true ) {
document.getElementById("abc").style.display = "block";
}
// what is the return good for?
// it would be better to return true or false
// or leave it away
return;
});
檢查jsFiddle:
現在,它會警告消息,但仍存在變量foundRecords
的問題。 保重。
編輯:
根據上面的評論,應該found
變量foundRecords
,因此關鍵行應為:
if ( found == true ) { // processing }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.