簡體   English   中英

如何使用jQuery .append()附加圖像?

[英]How to append image using jQuery .append()?

我可以在表格中添加一個字符串來顯示狀態

$('#test').empty().append("on")

但如果我能顯示圖像而不是字符串會更好。 我試過這個:

$('#test').empty().append(<img src="/static/on.png" height="64px" width="64px">)

但它不起作用。 我該怎么辦?

缺少報價:

$('#test').empty().append('<img src="/static/on.png" height="64px" width="64px">');

由於您正在清空和追加項目。 你可以這樣做。 .html將用圖像項替換選擇器的整個內容。 稍后,如果你想附加到這個div,你可以做.append(...);

$('#test').html('<img src="/static/on.png" height="64px" width="64px">');

如果您打算empty並附加圖像,那么.html()是最適合您的方法。

請參閱參考文獻.html()

嘗試

$('<img />', {
    src: 'test.png',
    width: '200px',
    height: '100px'
}).appendTo($('#empty').empty())

您需要在HTML周圍添加引號:

$('#test').empty().append('<img src="/static/on.png" height="64px" width="64px">');

我在這里使用單引號,因為你已經在HTML中有雙引號,所以如果你使用雙打來包圍它們,它們將轉義字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM