javascript - Remove 'onclick' command from a child of a div - Stack Overflow

I am working on a website where I have a main div. I wanted all of the div's page-area to act as a

I am working on a website where I have a main div. I wanted all of the div's page-area to act as a link (clicking on it will cause a submitting of a form and moving to another page) so I added to that div the attribute:

<div class="box1" onclick="javascript:document.forms['womenForm'].submit();" ...>

Everything in this area shuold link to the next page apart from an HTML selection that is inside this div, but should be clickable without moving to the next page.

How can I cause this to happen? I tried to wrap the selected element with a div, giving it href="" or onclick="" but still the form is submitted.

Can anybody resolve this?

I am working on a website where I have a main div. I wanted all of the div's page-area to act as a link (clicking on it will cause a submitting of a form and moving to another page) so I added to that div the attribute:

<div class="box1" onclick="javascript:document.forms['womenForm'].submit();" ...>

Everything in this area shuold link to the next page apart from an HTML selection that is inside this div, but should be clickable without moving to the next page.

How can I cause this to happen? I tried to wrap the selected element with a div, giving it href="" or onclick="" but still the form is submitted.

Can anybody resolve this?

Share Improve this question edited Jul 10, 2017 at 15:46 Wolfie 30.2k7 gold badges29 silver badges59 bronze badges asked Mar 6, 2010 at 11:09 SpidermanSpiderman 10k13 gold badges50 silver badges56 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You need to stop the bubbling of the event up the hierarchy ...

using the onclick attribute you can do this with

onclick="event.cancelBubble=true;if(event.stopPropagation) event.stopPropagation();return false;" on the select element.

First, the javascript: pseudo-protocol is superfluous in your code because you're using the "onclick" attribute.

I suggest moving away from inline JavaScript and venturing into the more acceptable unobtrusive JavaScript world.

Anyway, what you want to do is check that the event was not fired on a <select> element. With unobtrusive JS that would go something like this:

someElement.onclick = function(e) {
    var target = e ? e.target : event.srcElement;
    if (!/option|select/i.test(target.nodeName)) {
        // do stuff.
    }
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744247203a4564993.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信