1

在这里,我在 java 脚本中遇到一个错误 'div_element' 为空或不是对象。我在下面给出了我的代码

function showLoading(id) {
div_element = $("div#" + id)[0];
div_element.innerHTML = loading_anim; // Error in this line
}

当我调试我的脚本时,它在包括 IE 8 在内的其他浏览器中工作正常。但它在 IE 7 中不起作用。我不明白这个脚本中发生了什么确切问题。

提前致谢。

4

4 回答 4

0

I think you don't select anything with your jquery selector (line 2)

try to display

  • id
  • "div#" + id
  • $("div#" + id)
  • $("div#" + id)[0]

You can use firebug javascript console or a simple alert like this: alert($("div#" + id)[0]);

And see if you must id or class on your div ( use # or . selector)

于 2012-04-18T07:58:19.357 回答
0

First of all, you dont need to put a tag name infront of the jQuery, unless you have other elements with exact same id on other elements, in other pages.

Next, your statement div_element.innerHTML = loading_anim; is correct. So, the only explanation is that, there is no element with that ID, in the DOM.

Finally, since you are usign jQuery already, no need to mix up native JS and jQuery to create a dirty looking code.

function showLoading(id) {

    div_element = $("#" + id);
    console.log(div_element); //check the console to see if it return any element or not
    div_element.html(loading_anim);

}
于 2012-04-18T10:31:15.367 回答
0

用于.html()jQuery 对象。innerHTML适用于dom对象,它们不适用于 jQuery 对象。

function showLoading(id) {
    div_element = $("div#" + id);
    $(div_element).html(loading_anim); // Provided `loading_anim` is valid html element
}
于 2012-04-18T07:56:05.143 回答
0

我想,nic 想要显示一些加载器 GIF 动画,我确认,nic 必须.html()对 DOM 对象使用 jQuery 方法,并且 tusar 解决方案在 IE6+ 浏览器上运行良好。此外(很明显,但无论如何)nic 必须为loading_anim脚本中的变量赋值,比如说:var loading_anim = $('#loader').html();在将其值赋值给div_element.

于 2012-04-18T08:12:42.327 回答