3

div 内のすべての画像に特定のマージン プロパティを持たせたいのですが、うまく動作しないようです。これは私が試したものです。

$("#images > img").each(function(){
    $(img).css({
      margin-top:15px;
      margin-bottom:15px;
});
});

助けてくれてありがとう

$("#pt_figures").click(function() {

$('#images').empty();

$('#images').css({
paddingLeft: 150,
paddingRight: 0
});
$('#controls').css({
width:700,
marginLeft:150
});
$('#info').css({
width:660,
marginLeft:150

});

var id = $(this).attr('id');

$("#info_header").load(id +"_header.txt"); $("#content_1").load(id +"_1.txt"); $("#content_2").load(id +"_2.txt"); $("#content_3").load(id +"_3.txt");

$("<img>", { src: "http://www.klossal.com/figures_doc.jpg" }).appendTo("#images"); 
$("<img>", { src: "http://www.klossal.com/figure_front.png" }).appendTo("#images"); 
$("<img>", { src: "http://www.klossal.com/figure_back.jpg" }).appendTo("#images");


$("#images img").addClass("images");

$("#top_section").animate({
    height: 3780
}, 300);
$("#grid").animate({
    marginTop: 3780 + 300,
    paddingBottom: 300
}, 300); 


});
4

6 に答える 6

4

使用する $(this)

 $("#images > img").each(function(){
        $(this).css({
          margin-top:15px;
          margin-bottom:15px;
    });
    });
于 2012-11-26T12:11:25.840 に答える
2

推奨される方法は、jQueryコードにCSSを直接​​記述するのではなく、必要なプロパティを定義するクラスを追加することです。

Javascript:

$("#images img").addClass("myClass");

CSS:

.myClass {
   margin:15px 0;
}

または、CSSでプロパティを定義するだけで、jQueryは使用しないでください。

CSS:

#images img {
   margin:15px 0;
}
于 2012-11-26T12:14:00.280 に答える
1

これは、純粋なcssでも実行できます。

#images > img { // ">" means direct child, remove it if necesarry
  margin: 15px 0; // shorthand for top/bottom: 15px and right/left: 0px
}

>直接の子を意味します。つまり、#imagesの子である画像のみが影響を受け、「孫」は影響を受けません。それらが影響を受けることを意図している場合は、>

于 2012-11-26T12:14:07.487 に答える
1
$("#images > img").each(function(){
    $(this).css({
      margin-top:15px;
      margin-bottom:15px;
});
});

?

于 2012-11-26T12:11:05.960 に答える
1

これを試して:

$("#images img").each(function(){
    $(this).css({
        margin-top:15px;
        margin-bottom:15px;
    });
});

$(img) を使用していますが、コンテキストがありません。代わりに $(this) を使用してください。また、IMG タグが DIV の直接の子孫でない場合に備えて、セレクターから > を削除しました。

于 2012-11-26T12:11:39.523 に答える
0

に変更$(img)する$(this)と、準備が整います。

$(this)each ループ内の現在のオブジェクトです。

$("#images > img").css(...)なぜそれらすべてをループする必要があるのか​​ 直接わからないので、あなたも応募できるかもしれません。

于 2012-11-26T12:11:38.317 に答える