使用jquery动态创建div [英] using jquery to dynamically create div
本文介绍了使用jquery动态创建div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将背景图像添加到动态生成的div中.当我为背景添加CSS属性时,如下所示,它破坏了我的插件:
I am trying to add a background image to a dynamically generated div. When I add the CSS property for the background, like below, it breaks my plugin:
$("<div>", {
'class': "iviewer_image_mask",
css: "background: url('http://somesite.com/path/to/image.jpg');"
}).appendTo(this.container);
有人知道我在添加背景图片时做错了吗?
Anybody know what I am doing wrong to add the background image?
推荐答案
According to some benchmarking reported in this SO question, I believe the most efficient way to create your HTML element using jQuery would be this:
$('<div class="iviewer_image_mask" style="background: url(http://somesite.com/path/to/image.jpg);"></div>').appendTo(this.container);
或者为了增加可读性:
var elm = '<div class="iviewer_image_mask" ' +
'style="background: url(http://somesite.com/path/to/image.jpg);">'+
'</div>';
$(elm).appendTo(this.container);
这篇关于使用jquery动态创建div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文