使用 jQuery 将 img 更改为带背景的 div

发布于 2024-10-07 01:10:49 字数 396 浏览 6 评论 0原文

我想使用 jQuery 将具有特定类的所有 img 元素切换到具有背景的 div。 所以这一切:

<img class="specific" src="/inc/img/someimage.png" />

变成:

<div class="specificDiv" style="background: url(/inc/img/someimage.png); width: fromImageElementPX; height: fromImageElementPX;"></div>

我想这样做,以便我可以使用 css3 圆角。 CMS 用户(即客户)只能插入 IMG 元素。

提前致谢。

I'd like to use jQuery to switch all img elements with a specific class to a div with background.
So that all:

<img class="specific" src="/inc/img/someimage.png" />

Becomes:

<div class="specificDiv" style="background: url(/inc/img/someimage.png); width: fromImageElementPX; height: fromImageElementPX;"></div>

I'd like to do this so that I can round the corners using css3.
The CMS user, customer, will only be able to insert IMG elements.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空城旧梦 2024-10-14 01:10:49

我能想到的最简单的方法:

$('.specific').replaceWith(function () {
    var me = $(this);
    return '<div class="specificDiv" style="background: url(' + me.attr('src') + '); width: ' + me.width() + 'px; height: ' + me.height() + 'px;"></div>';
});

The easiest way I can think of:

$('.specific').replaceWith(function () {
    var me = $(this);
    return '<div class="specificDiv" style="background: url(' + me.attr('src') + '); width: ' + me.width() + 'px; height: ' + me.height() + 'px;"></div>';
});
夏至、离别 2024-10-14 01:10:49
$('img.specific').each(function(){ //iterate through images with "specific" class
    var $this = $(this), //save current to var
        width = $this.width(), //get the width and height
        height = $this.height(),
        img = $this.attr('src'), //get image source
        $div = $('<div class="specificDiv"></div>')
        .css({
            background: 'url('+img+')', //set some properties
            height: height+'px',
            width: width+'px'
        });
    $this.replaceWith($div); //out with the old, in with the new
})
$('img.specific').each(function(){ //iterate through images with "specific" class
    var $this = $(this), //save current to var
        width = $this.width(), //get the width and height
        height = $this.height(),
        img = $this.attr('src'), //get image source
        $div = $('<div class="specificDiv"></div>')
        .css({
            background: 'url('+img+')', //set some properties
            height: height+'px',
            width: width+'px'
        });
    $this.replaceWith($div); //out with the old, in with the new
})
篱下浅笙歌 2024-10-14 01:10:49

这应该有效,但我还没有测试过。

var origImage = $(".specific");
var newDiv = $("<div>").addClass("specificDiv");
newDiv.css("background-image", "url('" + origImage.attr("src") + "')");
newDiv.width(origImage.width()).height(origImage.height());
origImage.replaceWith(newDiv);

This should work, but I haven't tested it.

var origImage = $(".specific");
var newDiv = $("<div>").addClass("specificDiv");
newDiv.css("background-image", "url('" + origImage.attr("src") + "')");
newDiv.width(origImage.width()).height(origImage.height());
origImage.replaceWith(newDiv);
扭转时空 2024-10-14 01:10:49

另一种选择(从 Andrew Koester 获取代码)是将其放入插件中。它可能是这样的......

$.fn.replaceImage = function() {
  return this.each(function() {
    var origImage = $(this);
    var newDiv = $("<div>").attr("class", "specificDiv");
    newDiv.css("background", "url('" + origImage.attr("src") + "')");
    newDiv.width(origImage.width()).height(origImage.height());
    origImage.replaceWith(newDiv);  
  });
};

然后,要执行它,只需调用这样的东西......

$(".specific").replaceImage();

Another option (taking the code from Andrew Koester), is to put it in a plugin. Here's what it might look like...

$.fn.replaceImage = function() {
  return this.each(function() {
    var origImage = $(this);
    var newDiv = $("<div>").attr("class", "specificDiv");
    newDiv.css("background", "url('" + origImage.attr("src") + "')");
    newDiv.width(origImage.width()).height(origImage.height());
    origImage.replaceWith(newDiv);  
  });
};

Then, to execute it, just call something like this...

$(".specific").replaceImage();
回梦 2024-10-14 01:10:49

http://jsbin.com/ozoji3/3/edit

$(function() {
  $("img.rounded").each(function() {
    var $img = $(this),
        src = $img.attr('src'),
        w = $img.width(),
        h = $img.height(),
        $wrap = $('<span class="rounded">').css({
            'background-image': 'url('+ src +')',
            'height': h+'px',
            'width': w+'px'
          });
    $(this).wrap($wrap);
  });
});

http://jsbin.com/ozoji3/3/edit

$(function() {
  $("img.rounded").each(function() {
    var $img = $(this),
        src = $img.attr('src'),
        w = $img.width(),
        h = $img.height(),
        $wrap = $('<span class="rounded">').css({
            'background-image': 'url('+ src +')',
            'height': h+'px',
            'width': w+'px'
          });
    $(this).wrap($wrap);
  });
});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文