我一直在試圖讓一些jQuery運行替換對象的html。但是我怎麼能把一個if語句放在裏面,就像這樣。jquery if語句裏面的.html
$(this).html("<select id=\'status\' name=\'status[]\' multiple><option value=\'1\'"+if(string_contains(selected, 1)) { document.write(\' SELECTED\'); }+"></option></select>");
if語句使其停止工作。
還有一個小問題。
爲什麼不能在我裏面的.html打破這樣的:
$(this).html("
<select id=\'status\' name=\'status[]\' multiple>
<option value=\'1\'"+if(string_contains(selected, 1)) { document.write(\' SELECTED\'); }+"></option>
</select>");
JavaScript不允許在字符串常量(還)內換行符。 – Pointy
@Pointy:除非你至少用反斜槓轉義。 – Bergi
您正在尋找[三運營商](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Conditional_Operator)? – Bergi