0

私は状況を持っています:

<div id="main">
  <div id="a"></div>
  <div id="b"></div>
</div>

私の Web ページでは、要素Bは の下にありAます。B要素の上にあるA要素のみにクリックをバインドするにはどうすればよいですか? 例えば:

$('#main, #main > #b').click(function(){
  //something
});

EDIT2:このコードは、親 div または "b" 要素をクリックすると問題ありませんが、上にある "A" 要素をクリックすると何も起こりません。

一般に、B 要素のみをクリックしたいのですが、A 要素が BI の上にある場合は、イベントを発生させたいと考えています。ありがとう!

編集:残念ながら、HTML コードを変更することはできません。

4

3 に答える 3

1

あなたの質問にコメントを残したので、これをチェックしてください。たとえば、jqueryライブラリを使用して何を意味するかを示すスクリプトを作成しました。ここにリンクがあります

HTML:

<div id="main">
  <div id="a">A</div>
  <div id="b">B</div>
</div>

CSS:

#main div{width:100px;height:100px;display:block;position:absolute;margin:10px;top:0;left:0;line-height:100px;text-align:center;}
#main #a{background:#DDD;top:5px;left:5px;}
#main #b{background:#999;color:#FFF;}

jquery スクリプト:

//onHover B do the next...
$('#b').hover(function(){

  // actual B element move below
  $(this).css('zIndex', -1);

});
//onHover A do the next...
$('#a').hover(function(){
  // nothing on hover
},function(){
  // but onHoverOut do next...
  $('#b').css('zIndex', 'auto');
});
于 2012-09-24T13:55:30.560 に答える
0

おそらくこれはあなたが探しているものです:

HTML:

<div id="main">
  <div id="a">A</div>
  <div id="b">B</div>
  <div id="c">C</div>
</div>​

jQuery:

$(document).ready(function() {
    $("#main div").each(function() {

        // bind each div to the click event
        $(this).click(function() {
            alert("hah!");
        });

        // if we have reached "b", stop binding events to any following divs
        if ($(this).attr('id') == "b") {
          return false;
        }

    });
});

JSFiddle はこちら: http://jsfiddle.net/U6J8c/

于 2012-09-24T13:33:08.907 に答える
0

なぜあなたがこれを行っている のかわかりませんが、それにもかかわらず…</p>

$('#a').click(function(e) {
    e.preventDefault();
    $('#b').click();
});
于 2012-09-24T13:19:21.137 に答える