繁体   English   中英

Onload Javascript / jQuery无法在HTML中使用

[英]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:

http://jsfiddle.net/bx0e18L4/

现在,它会警告消息,但仍存在变量foundRecords的问题。 保重。

编辑:

根据上面的评论,应该found变量foundRecords ,因此关键行应为:

if ( found == true ) { // processing }

暂无
暂无

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

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