如何将样式(CSS)添加到<;img>;Javascript/Jquery中的标记
我在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
\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);
};