gpt4 book ai didi

Jquery Id 选择器不适用于 Js Id 选择器

转载 作者:太空宇宙 更新时间:2023-11-03 21:08:08 25 4
gpt4 key购买 nike

我制作了一个小程序来说明我遇到的实际问题。

  • 基本上,在这个程序中,我输入一个名字和一种颜色,然后按下提交按钮后,它们将附加到无序列表中。
  • 然后,我用 jquery id 选择器改变了句子的颜色,但是颜色没有改变,但 getElementById 工作正常。
  • 如果我输入 Tom 作为名字,红色作为颜色:应该输出一个红色的句子。

这里是 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;
}
<!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>

我在这里使用了 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;
}
<!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>

最佳答案

* 在选择器中有一个 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>

关于Jquery Id 选择器不适用于 Js Id 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50235726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com