javascript - 使用 jQuery 添加图像标签 - 如何包含类名?

标签 javascript jquery

我正在尝试通过以下 jQuery 添加 img 标签:

var parentElement = $("#sidebar");

    var img1 = $('<img />', { 
        id: 'headerImageLeft',
        src: '/Images/Header/FGM.jpg',
        class: 'img-responsive',
        style: 'float: left;',
        width: 80,
        alt: ''
    });
    img1.appendTo(parentElement);

虽然这似乎工作正常,但我不确定这是添加类的正确方法。在 Visual Studio 2013 中,我收到一条设计时警告,提示我正在使用 future 的保留字,这是添加类的正确方法吗?

最佳答案

根据jQuery docs ,因为 classreserved word ,需要引用。

Property names generally do not need to be quoted unless they are reserved words (as class is in this case).

所以:

var img1 = $('<img />', { 
    id: 'headerImageLeft',
    src: '/Images/Header/FGM.jpg',
    "class": 'img-responsive',
    style: 'float: left;',
    width: 80,
    alt: ''
});

关于javascript - 使用 jQuery 添加图像标签 - 如何包含类名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33619194/

相关文章:

javascript - Ajax 响应中的错误检查

jquery - 如何使 Twitter Bootstrap 按钮组与 AngularJS 一起工作?

javascript - 在 div 中嵌套选择菜单会破坏 jQuery 菜单

javascript - 如何在自定义过滤器函数中添加两个输入值

javascript - 如何从 iframe 的 $(document).ready 中选中父框架中的复选框?

javascript - jquery 从 6 种形式中只选择一个 radio

javascript - 更改 div 的宽度以匹配 span 标签的宽度

javascript - 如何在 Python 中使用 Javascript +Date() 数字时间戳?

javascript - OAuth 请求 token 错误 :401 in flickr

jquery - 仅对以特定字符开头的字符串使用 jquery 更改 css