[英]How can I get the id within a span represented as a string?
var span = '<span class="we w_o_r_d_s" id="we" contenteditable="false">we</span>'
console.log(span.id);
console.log(span.attr('id'));
console.log(span.getAttribute('id'));
var div = document.createElement('div');
div.innerHTML = span;
console.log(div.id);
console.log(div.attr('id'));
console.log(div.getAttribute('id'));
//I want to log `we`
我看了這里,但沒明白。 謝謝。
您可以使用jQuery
函數將字符串轉換為jQuery對象。 它將允許您在其上應用jQuery
selectors / functions
。
var span = jQuery ('<span class="we w_o_r_d_s" id="we" contenteditable="false">we</span>');
var id = span.attr('id');
代替span.attr('id')
使用$(span).attr('id')
您可能想將jQuery/$
功能包裝在DOM元素周圍的原因應該很明顯。 這樣做使您能夠使用jQuery鏈接(如果需要)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.