Javascript jQuery使用td值作为img的src
我尝试使用innerWrap将td值包装到图像中Javascript jQuery使用td值作为img的src,javascript,jquery,html,Javascript,Jquery,Html,我尝试使用innerWrap将td值包装到图像中 jQuery( "td[id^='imageList']" ).wrapInner( "<img></img>" ); jQuery( "td[id^='imageList']" ).wrapInner( "<img>" ); jQuery( "td[id^='imageList']" ).wrapInner( "<img src=''>" ); 变成 <img>http://sour
jQuery( "td[id^='imageList']" ).wrapInner( "<img></img>" );
jQuery( "td[id^='imageList']" ).wrapInner( "<img>" );
jQuery( "td[id^='imageList']" ).wrapInner( "<img src=''>" );
变成
<img>http://source.to/image.png</img>
成为
有人有解决这个问题的好办法吗?你可以用
使用
jQuery(“td[id^='imageList']”)。wrapInner(函数(){
返回“”;
});
您需要创建图像并更改源。为此,您可以使用:
<img src="http://source.to/image.png">
jQuery(“td[id^='imageList']”)。每个(函数(){
$this=jQuery(this);
var img=jQuery(“执行以下操作:
jQuery("td[id^='imageList']").wrapInner(function() {
return "<img src='"+ $(this).text() +"'></img>";
});
jQuery(“td[id^='imageList']”)。每个(函数(){
var$this=jQuery(this);
var src=jQuery.trim($this.text());
$this.html(“”);
});
修剪字符串很好,因为路径周围可能也有一些空格
例如:
顺便说一句,我可能会使用class
而不是id
示例#2:您希望从
中提取src
属性吗?您能向我们展示您的html吗?
<img src="http://source.to/image.png">
jQuery("td[id^='imageList']").wrapInner(function() {
return "<img src='"+ $(this).text() +"'></img>";
});
jQuery( "td[id^='imageList']" ).each(function(){
$this = jQuery(this);
var img = jQuery('<img>', {'src' : $this.text()});
$this.empty().append(img);
})
jQuery( "td[id^='imageList']" ).each(function(){
var $this = jQuery(this);
var src = jQuery.trim($this.text());
$this.html('<img src="'+ src +'">');
});