Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery:如何将css应用于.wrap元素?_Jquery_Jquery Selectors - Fatal编程技术网

JQuery:如何将css应用于.wrap元素?

JQuery:如何将css应用于.wrap元素?,jquery,jquery-selectors,Jquery,Jquery Selectors,到目前为止,我的代码是: $(".lorem img:not(.full-img)").each(function() { $(this).addClass("inline-img"); var imgWidth = $(this).prop('width'); var imgHeight = $(this).prop('height'); var imgFloat = $(this).prop('float'); $(this).wrap('<

到目前为止,我的代码是:

$(".lorem img:not(.full-img)").each(function() { 
    $(this).addClass("inline-img");
    var imgWidth = $(this).prop('width');
    var imgHeight = $(this).prop('height');
    var imgFloat = $(this).prop('float');

    $(this).wrap('<div class="inline-img-wrap" />'); 
        if ($(this).prop("title")!='') {
            $('<p class="inline-img-caption">').text($(this).prop('title')).insertAfter($(this)); 
        }
});
$(“.lorem img:not(.full img)”).each(function(){
$(this.addClass(“内联img”);
var imgWidth=$(this.prop('width');
var imgHeight=$(this.prop('height');
var imgFloat=$(this.prop('float');
$(此).wrap(“”);
如果($(此).prop(“标题”)!=“”){
$('

).text($(this.prop('title')).insertAfter($(this)); } });

我在文本体中搜索图像,将其
标题
值作为标题添加到下面,并将
img
p
包装在
div.inline-img-wrap
中。那很好

我需要的帮助是将
img
的样式应用于新的包装div。您可以看到,我已经为此创建了变量,但无法将它们应用到工作中
$此
将始终应用于图像,
不起作用


谢谢你的建议

我是否正确地告诉您需要检索新创建的包装元素

var $wrap = $(this).wrap('<div class="inline-img-wrap" />').parent();
if ($(this).prop("title")!='') {
    $('<p class="inline-img-caption">').text($(this).prop('title')).appendTo($wrap); 
}
// do any other manipulations with $wrap
var$wrap=$(this.wrap(“”).parent();
如果($(此).prop(“标题”)!=“”){
$('p class=“inline img caption”>).text($(this.prop('title')).appendTo($wrap);
}
//使用$wrap执行任何其他操作

另一方面,不要:缓存jQuery对象-您一直在调用$(这是次优的

尝试使用
数据
属性和
css
方法:

<img data-class='something'  src=''/>;

$(“.lorem img:not(.full img)”).each(function(){
$(this.addClass(“内联img”);
var imgWidth=$(this.css('width');
var imgHeight=$(this.css('height');
var imgFloat=$(this.css('float');
$(此).wrap(“”);
如果($(此).prop(“标题”)!=“”){
$('

).text($(this.prop('title')).insertAfter($(this)); } $(“+$(this.data('class')).css({“width”:imgWidth,`height`:imgHeight,`float`:imgFloat}) });


解决方案如下:我的代码在另一个国家,而不是optimal-我只是刚开始,所以它只在节假日访问optimal:)让我试试看,确保我了解它的机制……谢谢,你的代码实际上更接近我自己的目标,但Guard's现在对我来说很好:)
 $(".lorem img:not(.full-img)").each(function() { 
    $(this).addClass("inline-img");
    var imgWidth = $(this).css('width');
    var imgHeight = $(this).css('height');
    var imgFloat = $(this).css('float');

    $(this).wrap('<div class="inline-img-wrap + " " + $(this).data('class') + " />'); 
        if ($(this).prop("title")!='') {
            $('<p class="inline-img-caption">').text($(this).prop('title')).insertAfter($(this)); 
        }

    $("." + $(this).data('class')).css({"width": imgWidth, `height`: imgHeight, `float`: imgFloat})
});