[英]How can i remove HTML nbsp tags from select options when assigning the text of option to hidden input
[英]How can I get input tags with title attributes that contain " "?
我的 dom 中有以下輸入標簽,但無法添加類和/或 ID:
<input type="radio" title=" ">
當我嘗試
$('input[title=" "]')
或者
$('input[title=" "]')
它不返回任何東西。 我可以根據那個標題來拉這個嗎?
您的選擇器不起作用,因為您沒有使用正確的符號。 title 屬性獲取解碼的任何 html 實體,因此
將被解碼為實際符號
(ASCII 碼 160)。 所以這意味着
text 不會匹配它,常規空間也不會 (ASCII 碼 32)。
在 Windows 操作系統上,您可以鍵入ALT + 0160來獲取符號。 在其他操作系統上,您需要找到等效的組合鍵,或從字符映射中復制粘貼。
console.log( "Number of inputs with regular space: ", $('input[title=" "]').length ); console.log( "Number of inputs with text: ", $('input[title=" "]').length ); console.log( "Number of inputs with symbol: ", $('input[title=" "]').length );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!-- Regular space in title --> <input type="checkbox" title=" "> <!-- non-break space html entity --> <input type="checkbox" title=" "> <!-- encoded html entity which will match --> <input type="checkbox" title="&nbsp;">
將以下技巧與 JQuery 的filter()
函數和RegExp.test
函數結合使用,以獲取標題屬性包含
輸入標簽
:
var inputs = $("input[title]").filter(function(i){
return /\u00A0/.test($(this).attr('title'));
});
$("input[title*=' ']");
要獲取具有包含“常規”空間的標題屬性的輸入元素,請使用以下內容:
[attribute*=value]選擇器匹配其屬性值包含指定值的每個元素。
選擇所有非空白標題屬性就足夠了嗎?
$('input:not([title=""])').click(function(){ alert('non-blank title'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" title=" " />
這應該有效:
var isFound = $('[title=" "]').length === 1; if (isFound) console.log('Element is found.');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div title=" "></div>
這種細微的差別是把一個實際的
不是選擇器中的空格字符。
可以嘗試一個函數來返回 title 屬性的值並使用它,這有點笨拙。 如果您在普通頁面中使用它,您可以將下面的 getTitle 參數交換為更具體的選擇器,如 ID。
function getTitle(selector) {
return $(selector).attr('title');
}
$('input[title=' + getTitle('input[type="radio"]') + ']');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.