admin管理员组文章数量:1292620
function labelOnClick () {
function makeDivId(id) {
return id + "_div";
};
var div = this.getElementById(makeDivId(this.id));
if (div) {
div.parentNode.removeChild(div);
} else {
div = document.createElement("div");
div.innerHTML = "wele";
div.id = makeDivId(this.id);
this.appendChild(div);
}
}
<label id="1" onclick="labelOnClick()" > BROWSER </label>
<label id="2" onclick="labelOnClick()"> GAMING CONSOLE </label>
In the above example, I'm trying to create a div
element when a label is clicked and remove the created div
element when the label is clicked again, but it's not working.
function labelOnClick () {
function makeDivId(id) {
return id + "_div";
};
var div = this.getElementById(makeDivId(this.id));
if (div) {
div.parentNode.removeChild(div);
} else {
div = document.createElement("div");
div.innerHTML = "wele";
div.id = makeDivId(this.id);
this.appendChild(div);
}
}
<label id="1" onclick="labelOnClick()" > BROWSER </label>
<label id="2" onclick="labelOnClick()"> GAMING CONSOLE </label>
In the above example, I'm trying to create a div
element when a label is clicked and remove the created div
element when the label is clicked again, but it's not working.
-
You had two issues, the first problem is that
getElementById
is a method ofdocument
, notHTMLElement
. The second is incorrect use of the this keyword. Using inline events, you need to pass it as a argument, asthis
will refer to thewindow
. – circusbred Commented May 27, 2011 at 12:31
3 Answers
Reset to default 4You have several problems in your code:
When assigning event handlers inline (
label onclick="..."
) inside the event handler functionthis
will point to the global object (window
). You can passthis
as an argument to the function.Certain browsers will fail when assigning the result of
getElementById()
to a variable if no element is found (someone correct me if I'm wrong).
Something like this would work:
<script>
function labelOnClick (me) {
var makeDivId=function (id) {
return id + "_div";
};
var div;
if (document.getElementById(makeDivId(me.id))) {
div=document.getElementById(makeDivId(me.id));
div.parentNode.removeChild(div);
} else {
div = document.createElement("div");
div.innerHTML = "wele";
div.id = makeDivId(me.id);
me.appendChild(div);
}
}
</script>
<label id="1" onclick="labelOnClick(this)" > BROWSER </label>
<label id="2" onclick="labelOnClick(this)"> GAMING CONSOLE </label>
jsFiddle Demo
My suggestion would be to not add the div using the javascript, but to change the div tag's visibility style property on click..
function labelOnClick () {
function makeDivId(id) {
return id + "_div";
};
//var div = this.getElementById(makeDivId(this.id));
if (document.getElementById("divID").style.visibility == "visible") {
document.getElementById("divID").style.visibility = "hidden";
} else {
document.getElementById("divID").style.visibility = "hidden";
}
}
<label id="1" onclick="labelOnClick()" > BROWSER </label>
<label id="2" onclick="labelOnClick()"> GAMING CONSOLE </label>
You are trying to use the "this" keyword in place of "document", which does not work since "this" is pointing to the labelOnClick function.
function labelOnClick(e)
{
function makeDivId(id)
{
return id + "_div";
};
var div = document.getElementById(makeDivId(this.id));
if (div)
{
div.parentNode.removeChild(div);
}
else
{
div = document.createElement("div");
div.innerHTML = "wele";
div.id = makeDivId(this.id);
var element = e.target;
element.parentNode.insertBefore(div, element.nextSibling);
}
}
<label id="1" onclick="labelOnClick(event)" > BROWSER </label>
<label id="2" onclick="labelOnClick(event)"> GAMING CONSOLE </label>
I wrote this assuming you are using something that supports the "target" property of the event object (e.g. not internet explorer). If you need cross browser support, you can do it manually or use a framework like jQuery or Prototype.
The way the original code was written, I assumed that you wanted a div per label and I guessed at the positioning (immediately following the label).
本文标签: dom eventsCreating and removing ltdivgt element in JavascriptStack Overflow
版权声明:本文标题:dom events - Creating and removing <div> element in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741558910a2385329.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论