getByElementId hide?

China☆狼群 提交于 2019-12-13 03:14:08

问题


document.getElementById(id).hide("slide", { direction: "down" }, 1000);

This is in my javascript file.

I'm trying to hide a div, which has the id of var 'id'.

The error console is listing that it isn't a function.

Edit:

I don't think I'm getting the right element this way (using $("#" + id)).

Let's say im making the div like this: <div id="2">, and the javascript function is getting 2


回答1:


document.getElementById(id) is Dom element. and it has no function hide

You have to use $('#'+id).hide. Or $(document.getElementById(id)).hide




回答2:


$("#" + id).hide(...);

Don't use getElementById if you have jQuery.

Also, to fix your initial code, here's how it should have looked like:

$(document.getElementById(id)).hide(...);

This is because jQuery doesn't extend DOM elements, but it can "convert" them into jQuery elements (which have those cute functions) with $().




回答3:


When using document.getElementById(..) you cannot use jQuery functions like hide(..). Use the jQuery shorthand - $("#" + id)



来源:https://stackoverflow.com/questions/6825940/getbyelementid-hide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!