4

これは、jQueryを使用して表示ブロックを持つレポート内のすべての要素を選択したいhtmlコンテンツ $("#report:visible")です。

<div id="report">
        <div id="p1" style="display: block;">
            <input id="pname1"  type="checkbox" name="report1">
            <input id="pname2"  type="checkbox" name="report2">
        </div>
        <div id="p2"  style="display: none;">
            <input id="pname1"  type="checkbox" name="report1">
            <input id="pname2"  type="checkbox" name="report2">
        </div>
        <div id="p3"  style="display: none;">
            <input id="pname1"  type="checkbox" name="report1">
            <input id="pname2"  type="checkbox" name="report2">
        </div>
            <div id="p4"  style="display: block;">
            <input id="pname3"  type="checkbox" name="report1">
            <input id="pname4"  type="checkbox" name="report2">
        </div>
</div>
4

9 に答える 9

19

多分あなたはこのjQueryの一部を使うことができます:

$("#report div:visible").each(function() { 
    console.log($(this).attr('id')); 
});

またはこれ:)?

$("#report div:visible");
于 2013-01-18T12:10:30.703 に答える
4
$("#report > :visible") 

This will select the direct children of #report that are visible. Without the space you're selecting #report itself if it's visible. (Without the > it'd target also the inputs.)

于 2013-01-18T11:59:04.727 に答える
2

これは、いくつかのセレクターCSS セレクターで役立つ場合があります。

要件については、これを使用しての下ですべてdivを選択できます。display:block#report

$('#report div[style*=display:block]')
于 2013-01-18T12:02:01.347 に答える
1

You cannot directly select elements in CSS using a property value itself. You can however select by class. The best solution would be to use a class to assign display: block (such as a visible class) and then to select based on its presence or lack thereof.

The other way to do this is to select using the entire value of the style element. But the problem with this is that if you add other inline styles that selector will no longer work. You could then get into regex parsing the style attribute but in my opinion applying a visible or hidden class is far easier and will perform significantly better.

Note that another advantage of using the visible or hidden class is that you can turn it on and off with JavaScript very easily:

document.getElementById("id").classList.toggle("hidden");
于 2013-01-18T11:59:46.827 に答える
1

あなたが使用することができます:

$("[style='display: block;']");

フックするクラスも追加します。

于 2013-01-18T11:58:24.447 に答える
0

なぜだけではないのですか

$('#report div:visible');

マークアップがそのようにとどまる場合、それは機能します。「エントリ」のようなレポートエントリにクラスを追加するだけではない場合は、

$('#report .entry:visible');
于 2013-01-18T12:10:50.810 に答える