Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.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
如何将样式(CSS)添加到<;img>;Javascript/Jquery中的标记_Javascript_Jquery_Html_Css - Fatal编程技术网

如何将样式(CSS)添加到<;img>;Javascript/Jquery中的标记

如何将样式(CSS)添加到<;img>;Javascript/Jquery中的标记,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我在jquery中有以下\u renderItem函数,其中有“+”“+”“+项.标签+”) .附录(ul); }; 这应该适合您 .data('autocomplete')._renderItem = function(ul, item) { return $('<li>') .data('item.autocomplete', item) .append("&l

我在jquery中有以下
\u renderItem
函数,其中有
“+”“+”“+项.标签+”)
.附录(ul);
}; 

这应该适合您

.data('autocomplete')._renderItem = function(ul, item) {
         return $('<li>')                                  
        .data('item.autocomplete', item)
        .append("<a>"+"<img src ='/account/"+item.id+"/icon/logo' onerror='$(this).hide()' height='40' width='40' alt=''/>" + " " + " " +item.label+"</a>")
        .appendTo(ul); 

 }; 
.data('autocomplete')。\u renderItem=函数(ul,项目){
变量imgLink=$(''+item.label+'')
.append($('')
.data('item.autocomplete',item)
.append(imgLink)
.附录(ul);
};

您可以替换内联属性

.data('autocomplete')._renderItem = function (ul, item) {
    var imgLink = $('<a>' + item.label + '</a>')
        .append($('<img />', {
            'src': '/account/' + item.id + '/icon/logo',
            'style': 'width:40px; height:40px;',
            onerror: function() {
                $(this).hide();
            }
        }));

    return $('<li>')
        .data('item.autocomplete', item)
        .append(imgLink)
        .appendTo(ul);

};
使用像这样的内联css

  height='40' width='40' 
.data('autocomplete')。\u renderItem=函数(ul,项目){
返回$(“
  • ”) .data('item.autocomplete',item) .append(“+”+“+”+项.标签+”) .附录(ul); };
  • 那么你是说
    height='40'width='40'
    在IE之外的任何地方都能工作?是的,内联属性在IE中不起作用,不幸的是,IE会要求它们用CSS方式编写?为什么你不能编写style='height:40px;width:40px'而不是内联属性呢?这里我使用内联属性来调整图像大小(通常在chrome、mozilla等中工作)但在IE中,它们应该像内联样式一样编写才能工作。但这是javascript,我不确定谁来编写样式。这是在
    'onerror':'$(this).hide()处发现解析错误“,
    line这是因为我错过了
    onerror
    函数之前的逗号,现在修复了此问题!谢谢!我只是不知道如何在javascript中使用这些内联样式。
        .data('autocomplete')._renderItem = function(ul, item) {
                 return $('<li>')                                  
                .data('item.autocomplete', item)
                .append("<a>"+"<img src ='/account/"+item.id+"/icon/logo' onerror='$(this).hide()' style='width:40px;height:40px' alt=''/>" + " " + " " +item.label+"</a>")
                .appendTo(ul); 
    
         };