我做了一个小程序来说明我遇到的真正问题。
getElementById
工作正常。这是jquery选择器版本:
$(document).ready(function () {
// initialization
$('select').formSelect();
$('#myForm').submit(function (e) {
let name = $('#name').val();
let color = $('#color').val();
addToList(name,color);
e.preventDefault();
});
});
function addToList (n,c) {
$('#main').append(`<li id=${n}**${c}>My name is ${n}. My favorite color is: ${c}</li>`);
$(`#${n}**${c}`).css({'color':`${c}`});
// document.getElementById(`${n}**${c}`).style.color = c;
}
&#13;
<!DOCTYPE html>
<html>
<head>
<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My Lab</title>
</head>
<body>
<div class="container">
<form id="myForm">
<div class="input-field">
<label for="name">Name</label>
<input type="text" id="name" required>
</div>
<div class="input-field">
<label for="color">Color</label>
<input type="text" id="color" required>
<button class="btn" type="submit">Submit</button>
</div>
<ul id="main"></ul>
</form>
</div>
</body>
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
<script src="app.js"></script>
</html>
&#13;
我在这里使用了getElementById
,它的确有效:
$(document).ready(function () {
// initialization
$('select').formSelect();
$('#myForm').submit(function (e) {
let name = $('#name').val();
let color = $('#color').val();
addToList(name,color);
e.preventDefault();
});
});
function addToList (n,c) {
$('#main').append(`<li id=${n}**${c}>My name is ${n}. My favorite color is: ${c}</li>`);
// $(`#${n}**${c}`).css({'color':`${c}`});
document.getElementById(`${n}**${c}`).style.color = c;
}
&#13;
<!DOCTYPE html>
<html>
<head>
<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My Lab</title>
</head>
<body>
<div class="container">
<form id="myForm">
<div class="input-field">
<label for="name">Name</label>
<input type="text" id="name" required>
</div>
<div class="input-field">
<label for="color">Color</label>
<input type="text" id="color" required>
<button class="btn" type="submit">Submit</button>
</div>
<ul id="main"></ul>
</form>
</div>
</body>
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
<script src="app.js"></script>
</html>
&#13;
答案 0 :(得分:2)
您的id
是有效的HTML id
,但不有效literal ID in a CSS ID selector(#xxx
)。
我建议强烈不使用最终用户输入来形成您的元素ID。
在这种情况下,要在仍然使用该ID格式的情况下使用jQuery,您可以:
在其上使用getElementById
然后$()
:
$(document.getElementById(`${n}**${c}`)).css({'color':`${c}`});
或
请改用属性选择器:
$(`[id="${n}**${c}:]`)).css({'color':`${c}`});`,
旁注:如果您愿意,.css({'color':
$ {c} })
可以写成.css('color',
$ {c} )
。
答案 1 :(得分:2)
*
有一个special meaning。你需要逃脱它。使用$.escapeSelector()
来执行此操作。替换这个:
$(`#${n}**${c}`).css({'color':`${c}`});
用这个:
$(`#${$.escapeSelector(`${n}**${c}`)}`).css({'color':`${c}`});
工作演示:
$(document).ready(function() {
// initialization
$('select').formSelect();
$('#myForm').submit(function(e) {
let name = $('#name').val();
let color = $('#color').val();
addToList(name, color);
e.preventDefault();
});
});
function addToList(n, c) {
$('#main').append(`<li id=${n}**${c}>My name is ${n}. My favorite color is: ${c}</li>`);
$(`#${$.escapeSelector(`${n}**${c}`)}`).css({
'color': `${c}`
});
// document.getElementById(`${n}**${c}`).style.color = c;
}
<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My Lab</title>
</head>
<body>
<div class="container">
<form id="myForm">
<div class="input-field">
<label for="name">Name</label>
<input type="text" id="name" required>
</div>
<div class="input-field">
<label for="color">Color</label>
<input type="text" id="color" required>
<button class="btn" type="submit">Submit</button>
</div>
<ul id="main"></ul>
</form>
</div>
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
<script src="app.js"></script>