我有以下表中的TD文本(8" & 12 「)的jQuery包含選擇不倒逗號和breackt工作
我的代碼是
var match = $('#table_body > tr > td:contains("' + searchterm + '")');
,當我試圖找到(8」 & 12 「)這得到以下錯誤
錯誤:語法錯誤,不能識別的表達式::含有(」 「)」
最後「)雙字符s正在創建問題它是什麼解決方案我嘗試了轉義字符串(添加斜線)錯誤消失了,但沒有搜索結果與此
我有以下表中的TD文本(8" & 12 「)的jQuery包含選擇不倒逗號和breackt工作
我的代碼是
var match = $('#table_body > tr > td:contains("' + searchterm + '")');
,當我試圖找到(8」 & 12 「)這得到以下錯誤
錯誤:語法錯誤,不能識別的表達式::含有(」 「)」
最後「)雙字符s正在創建問題它是什麼解決方案我嘗試了轉義字符串(添加斜線)錯誤消失了,但沒有搜索結果與此
它適用於單引號圍繞您的td:contains('some text with "quotes"')
。該文檔不會使用雙引號列出作爲有效的選擇。請注意,你並不需要周圍的引號,你可以使用單詞。無論您的文本是包含單引號還是雙引號,它都可以工作。
var match = $("#table_body > tr > td:contains('" +searchterm+ "')");
// or without quotes altogether
var match = $("#table_body > tr > td:contains(" +searchterm+ ")");
例子:
var text = '8" 12"';
var text2 = '5" 6"'
var text3 = '2\' 4"';
var text4 = "Testing (1' 3')";
// Quotes are allowed
$("div:contains('"+text+"')").css('background-color', 'red');
// But not necessary
$("div:contains("+text2+")").css('background-color', 'yellow');
// Works with mixed single and double quotes
$("div:contains("+text3+")").css('background-color', 'gray');
// However, it doesn't support parentheses, you'll have to search it yourself
// And make sure you escape regex special characters
// Escapes any special characters
RegExp.escape = function(s, flags) {
return new RegExp(s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), flags);
};
console.log(RegExp.escape("1' 3')"))
$('div').filter(function(i, el) {
return $(el).text().match(RegExp.escape("1' 3')"));
}).css('background-color', '#fef0e4');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>8" 12"</div>
<div>5" 6"</div>
<div>2' 4"</div>
<div>1' 3')</div>
我試過但不工作 var searchterm ='「)'; $('#table_body> tr> td:contains(」'+ searchterm.replace(/ 「/ g,」\\\「」)+'「)'); –
@NavneetGarg最初的嘗試沒有奏效,但我添加了一個工作示例的答案 –
是的這是工作謝謝,但我想知道如果用戶鍵入')這比這個代碼生成錯誤 –
你可以張貼HTML? –
\t \t
https://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-已經使用了-css-notation/ – epascarello