問題
es5怎么在html里寫三元表達(dá)式使用不同的樣式,如下圖和代碼
代碼
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
Hello ECMA Script 6
<div id="test">a</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var b = 5;
var c = 6;
var a = "<div " + (b > c) + " ? style='color:red' : style='color:blue'></div>";
$("#test").html(a)
</script>
</body>
</html>
6 回答

慕桂英4014372
TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個(gè)贊
var a = b > c ? "<div style='color:red'></div>":"<div style='color:blue'></div>";
這樣?
或者
var color = b > c ? 'red': 'blue';
var a = '<div style="color:'+color+'"></div>';

ibeautiful
TA貢獻(xiàn)1993條經(jīng)驗(yàn) 獲得超6個(gè)贊
方法1:
var a = "<div style=\"${b > c?'color:red':'color:blue'}\"></div>";
方法2:
var a = "<div style=\""+(b>c?"color:red":"color:blue")+"\"></div>";

皈依舞
TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊
var b = 5;
var c = 6;
$("<div>")
.text("123")
.css("color", b > c ? "red" : "blue")
.appendTo("#test");
- 6 回答
- 0 關(guān)注
- 3174 瀏覽
添加回答
舉報(bào)
0/150
提交
取消