js中的事件处理

发布于 2024-11-01 16:42:29 字数 2004 浏览 2 评论 0原文

我在其单击上有一个div

<div id="mainDiv" onclick="devide(this.id, event);" style="z-index:1;position:relative; width:400px; height:400px; border:1px red solid;"></div>

,我向其追加了更多div,并希望它们发生同样的事情,意味着在那里单击另外2个div,

然后单击mainDiv 将其添加到它们中 主分区 -mainDiv0 -mainDiv1

单击 mainDiv0 主分区 -mainDiv0 -mainDiv00 -mainDiv01 -mainDiv1

我想我能够清除

我在js之后使用的

function devide(id, evt){
    alert(id);
    var divElement = document.getElementById(id);
    if(typeof(divElement) != "undefined" && divElement!=null){
        captureMousePosition(evt);
        if(splitVertical==1)
            verticalSplit(divElement);
        else
            horizontalSplit(divElement);
    }
}

function verticalSplit(divElement){
    divElement.style.border = "0px red dashed";
//  divElement.removeAttribute("onclick");
    var width = ((divElement.offsetWidth-(xMousePos-LEFT_MARGIN))/divElement.offsetWidth)*100;
    var newDiv1 = document.createElement("div");
    newDiv1.setAttribute("id", divElement.id+"0");
//  newDiv1.onclick = "devide('"+divElement.id+"0', event);";
    newDiv1.style.width = (100-width)+"%";
    newDiv1.style.height = "100%";
    newDiv1.style.float = "left";
    newDiv1.style.border = "1px red dashed";
    newDiv1.style.zIndex = divElement.style.zIndex+1;
    var newDiv2 = document.createElement("div");
    newDiv2.setAttribute("id", divElement.id+"1");
    newDiv2.style.width = width+"%";
    newDiv2.style.height = "100%";
    newDiv2.style.float = "left";
    newDiv2.style.border = "1px red dashed";
    newDiv2.style.zIndex = divElement.style.zIndex+1;
    divElement.appendChild(newDiv1);
    divElement.appendChild(newDiv2);
    newDiv1.addEventListener("click",devide(divElement.id+"0", event),true);
    newDiv2.addEventListener("click",devide(divElement.id+"1", event),true);
}

东西,但是这是递归调用第一个遇到的元素的devide函数,例如mainDiv0,mainDiv00,mainDiv000....等等,

请帮助我

i have a div

<div id="mainDiv" onclick="devide(this.id, event);" style="z-index:1;position:relative; width:400px; height:400px; border:1px red solid;"></div>

on its click i append to more divs to it and want the same thing to happen to them, means on there click 2 more divs shud b added to them

on click on mainDiv
mainDiv
-mainDiv0
-mainDiv1

on click on mainDiv0
mainDiv
-mainDiv0
-mainDiv00
-mainDiv01
-mainDiv1

i guess i was able to clear the things

i used following js

function devide(id, evt){
    alert(id);
    var divElement = document.getElementById(id);
    if(typeof(divElement) != "undefined" && divElement!=null){
        captureMousePosition(evt);
        if(splitVertical==1)
            verticalSplit(divElement);
        else
            horizontalSplit(divElement);
    }
}

function verticalSplit(divElement){
    divElement.style.border = "0px red dashed";
//  divElement.removeAttribute("onclick");
    var width = ((divElement.offsetWidth-(xMousePos-LEFT_MARGIN))/divElement.offsetWidth)*100;
    var newDiv1 = document.createElement("div");
    newDiv1.setAttribute("id", divElement.id+"0");
//  newDiv1.onclick = "devide('"+divElement.id+"0', event);";
    newDiv1.style.width = (100-width)+"%";
    newDiv1.style.height = "100%";
    newDiv1.style.float = "left";
    newDiv1.style.border = "1px red dashed";
    newDiv1.style.zIndex = divElement.style.zIndex+1;
    var newDiv2 = document.createElement("div");
    newDiv2.setAttribute("id", divElement.id+"1");
    newDiv2.style.width = width+"%";
    newDiv2.style.height = "100%";
    newDiv2.style.float = "left";
    newDiv2.style.border = "1px red dashed";
    newDiv2.style.zIndex = divElement.style.zIndex+1;
    divElement.appendChild(newDiv1);
    divElement.appendChild(newDiv2);
    newDiv1.addEventListener("click",devide(divElement.id+"0", event),true);
    newDiv2.addEventListener("click",devide(divElement.id+"1", event),true);
}

but this is recursively calling devide function for the first encountered element say mainDiv0,mainDiv00,mainDiv000.... and so on

please help me with this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隔纱相望 2024-11-08 16:42:29

您描述的情况称为事件冒泡

W3C 模型表示您可以对事件调用 .stopPropagation()。 IE 可能需要 window.event.cancelBubble

来执行不引人注目的 javascript代码尝试:

HTML

<div id="mainDiv"></div>

JS

function devide(evt){
    var divElement = document.getElementById(this.id);// this.id !
    if(typeof(divElement) != "undefined" && divElement!=null){
        captureMousePosition(evt);
        if(splitVertical==1)
            verticalSplit(divElement);
        else
            horizontalSplit(divElement);
    }
}

document.getElementById('mainDiv').onclick=devide; // notice it's just the function name

The situation you are describing is called event bubbling.

The W3C model says you can call .stopPropagation() on the event. IE might need window.event.cancelBubble

To do unobtrusive javascript with your code try:

HTML

<div id="mainDiv"></div>

JS

function devide(evt){
    var divElement = document.getElementById(this.id);// this.id !
    if(typeof(divElement) != "undefined" && divElement!=null){
        captureMousePosition(evt);
        if(splitVertical==1)
            verticalSplit(divElement);
        else
            horizontalSplit(divElement);
    }
}

document.getElementById('mainDiv').onclick=devide; // notice it's just the function name
白云不回头 2024-11-08 16:42:29

关于以下几行:

var divElement = document.getElementById(id);
if(typeof(divElement) != "undefined" && divElement!=null){

typeof 是一个运算符,而不是一个函数,因此它后面不需要跟分组运算符 ()。

更重要的是,测试的第一部分是不必要的,第二部分可以简化为:

if (divElement) {

如果 document 对象不存在,或者不支持 getElementById 则第一行将导致脚本错误并停止执行。如果第一行成功执行,则 divElement 将是对 DOM 元素的引用或 null(根据 W3C DOM 核心规范)。

鉴于上述情况,如果 divElement != null 返回 true,则 typeof divElement != 'undefined' 必须返回 true(因为 divElement 不能是未定义的,即使它是,未定义== null),反之亦然,所以它是多余的。如果需要显式测试(很少需要),则应

if (divElement !== null) {

使用:。

Regarding the lines:

var divElement = document.getElementById(id);
if(typeof(divElement) != "undefined" && divElement!=null){

typeof is an operator, not a function, so it doesn't need to be followed by a grouping operator ().

More importantly, the first part of the test is unnecessary and the second part can be reduced to:

if (divElement) {

If a document object doesn't exist, or does not support getElementById then the first line will result in a script error and stop execution. If the first line executes successfully, then divElement will either be a reference to a DOM element or null (per the W3C DOM Core specification).

Given the above, if divElement != null returns true, then typeof divElement != 'undefined' must return true (since divElement can't be undefined and even if it was, undefined == null), and vice versa, so it is redundant. If an explicit test is required (and it rarely is), then:

if (divElement !== null) {

should be used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文