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

标签 jquery html css

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

  • 基本上,在这个程序中,我输入一个名字和一种颜色,然后 按下提交按钮后,它们将附加到无序列表中。
  • 然后,我用 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/

相关文章:

javascript - 根据选中的复选框返回一个字符串

javascript - 如何解决 JS/Jquery 动画代码流错误?

html - 使用滚动使多个表格在一行中显示

css - Vue2 : v-move not applied to "leave" transition

javascript - IE7+ 'Shrink to Fit' 使文本太小

javascript - 有没有办法用ajax获取无限网页?

javascript - Rails 中数据表的替代品?

jquery - 查询 Javascript 库

javascript - 如何防止 awesomplete 破坏布局

html - 样式内联图像和标题