Jquery Id选择器无法对Js Id选择器

时间:2018-05-08 14:11:34

标签: 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;
}
&#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;
&#13;
&#13;

我在这里使用了getElementById,它的确有效:

&#13;
&#13;
$(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;
&#13;
&#13;

2 个答案:

答案 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>