jQuery id 选择器正在选择所有元素

Posted

技术标签:

【中文标题】jQuery id 选择器正在选择所有元素【英文标题】:jQuery id selector is selecting all elements 【发布时间】:2020-02-05 18:14:41 【问题描述】:

我正在尝试选择具有特定 id (todoItems) 的 ul,但页面上的所有 ul 都受到此代码的影响。特别是 ul 在我的导航栏中。我也尝试按班级选择它们,但没有运气。当我附加到 ul 时,我想不出一种专门选择 li 的方法。任何帮助表示赞赏。我的代码见下文:

EDIT* 这也是这个的 css。我在一个快速应用程序中运行所有这些代码。我在这个应用程序的一个页面上运行这个待办事项列表,它有自己的导航栏等。CSS/JS 等使用 ejs 布局链接。

var todoItems = document.getElementById("todoItems");

$(todoItems).on("click", ".todoItem", function() 
	$(this).toggleClass("completed");
);

$(todoItems).on("click", "span.remove", function(event) 
	$(this).parent().fadeOut(300, function() 
		$(this).remove();
	);
	event.stopPropagation();
);

$("input[type='text']").on("keypress", function(event) 
	if (event.which === 13)
		var todoText = $(this).val();
		$(todoItems).append("<li class='todoItem'><span class = 'remove'><i class='fa fa-trash'></i> </span>" + todoText + "</li>");
	
);

$(".fa-plus").on("click", () => 
	$("input[type='text']").fadeToggle();
);
body 
    background-color: #43515f;


.siteBody 
	font-family: 'Roboto', sans-serif;
	background: #2980B9;  /* fallback for old browsers */
	background: -webkit-linear-gradient(to left, #FFFFFF, #6DD5FA, #2980B9);  /* Chrome 10-25, Safari 5.1-6 */
    background: linear-gradient(to left, #FFFFFF, #6DD5FA, #2980B9); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
    padding-top: 10px;
    padding-bottom: 10px;


.siteBody h1 
	background-color: #2980b9;
	color: white;
	margin: 0;
	padding: 10px 20px;
	text-transform: uppercase;
	font-size: 24px;
	font-weight: normal;


input 
	font-size: 18px;
	background-color: #f7f7f7;
	width: 100%;
	padding: 13px 13px 13px 20px;
	color: #2980b9;
	border: 3px solid rgba(0, 0, 0, 0);


input:focus 
	background-color: white;
	border: 3px solid #2980b9;
	outline: none;


.siteBody ul 
	list-style: none;
	margin: 0;
	padding: 0;


.siteBody li 
	background-color: white;
	height: 40px;
	line-height: 40px;
	color: #666;


.siteBody li:nth-child(2n) 
	background-color: #f7f7f7;


.siteBody li:hover span 
	width: 40px;
	opacity: 1.0;


.siteBody span 
	background-color: #e74c3c;
	height: 40px;
	margin-right: 20px;
	text-align: center;
	color: white;
	width: 0;
	display: inline-block;
	transition: 0.2s linear;
	opacity: 0;


.fa-plus 
	float: right;


.completed 
	color: gray;
	text-decoration: line-through;


#container 
	background-color: #f7f7f7;
	box-shadow: : 0 0 3px rgba(0, 0, 0, 0.1);
	width: 360px;
	margin: 100px auto;
    <nav id="mainNav" class="navbar navbar-expand-lg navbar-dark">
        <div class="container">
          <a class="navbar-brand" href="#">Trey Cooper</a>
          <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mainNavbar" aria-controls="mainNavbar" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
  
          <div class="collapse navbar-collapse" id="mainNavbar">
            <ul class="navbar-nav mr-auto">
              <li class="nav-item">
                <a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="/posts">Projects</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="/posts/new">New Post</a>
              </li>
            </ul>
              <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                  <a class="nav-link" href="https://www.linkedin.com/in/trey-cooper-8a231a123/" target="_blank"><i class="fa fa-linkedin fa-lg white-text fa-2x"></i></a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="https://github.com/cooper6101" target="_blank"><i class="fa fa-github fa-2x"></i></a>
                </li>
              </ul>
          </div>
        </div>
      </nav>
      
<div class="container-fluid siteBody">
    <div id="container">
        <h1>TO-DO List <i class="fa fa-plus"></i></h1>
        <input type="text" name="addTodo" placeholder="Add New Todo">
        <ul id="todoItems">
            <li class="todoItem"><span class="remove"><i class="fa fa-trash"></i></span> Grocery Shop</li>
            <li class="todoItem"><span class="remove"><i class="fa fa-trash"></i></span> Get coding job</li>
            <li class="todoItem"><span class="remove"><i class="fa fa-trash"></i></span> Finish both Bootcamps</li>
            <li class="todoItem"><span class="remove"><i class="fa fa-trash"></i></span> Walk Fish</li>
            <li class="todoItem"><span class="remove"><i class="fa fa-trash"></i></span> Cook Dinner</li>
            <li class="todoItem"><span class="remove"><i class="fa fa-trash"></i></span> Book a flight</li>
        </ul>
    </div>
</div>

【问题讨论】:

只要使用$('#todoItems') 我最初尝试过,但不幸遇到了同样的问题 也发布您的 CSS。我们需要minimal reproducible example,但我没有看到。 刚刚添加了css 【参考方案1】:

请试试这个代码:

var todoItems = document.getElementById("todoItems");

    $(document).on("click", "#todoItems .todoItem", function() 
        $(this).toggleClass("completed");
    );

    $(document).on("click", "#todoItems span.remove", function(event) 
        $(this).parent().fadeOut(300, function() 
            $(this).remove();
        );
        event.stopPropagation();
    );

    $("input[type='text']").on("keypress", function(event) 
        if (event.which === 13) 
            var todoText = $(this).val();
            $(todoItems).append("<li class='todoItem'><span class = 'remove'><i class='fa fa-trash'></i> </span>" + todoText + "</li>");
        
    );

    $(".fa-plus").on("click", () => 
        $("input[type='text']").fadeToggle();
    );

【讨论】:

谢谢,但这仍然不起作用。我正在使用布局的快速应用程序中运行此代码。我想知道这是否会导致我的问题。【参考方案2】:

我已经更新了你设置事件监听器的方式,并删除了纯 js 选择元素的方式,因为你已经使用了 jQuery。

$("#todoItems > .todoItem").on("click", function() 
  $(this).toggleClass("completed");
);

$("#todoItems > span.remove").on("click", function(event) 
  $(this).parent().fadeOut(300, function() 
    $(this).remove();
  );
  event.stopPropagation();
);

$("input[type='text']").on("keypress", function(event) 
  if (event.which === 13) 
    var todoText = $(this).val();
    $("#todoItems").append("<li class='todoItem'><span class = 'remove'><i class='fa fa-trash'></i> </span>" + todoText + "</li>");
  
);

$(".fa-plus").on("click", () => 
  $("input[type='text']").fadeToggle();
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav id="mainNav" class="navbar navbar-expand-lg navbar-dark">
  <div class="container">
    <a class="navbar-brand" href="#">Trey Cooper</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mainNavbar" aria-controls="mainNavbar" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="mainNavbar">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item">
          <a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="/posts">Projects</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="/posts/new">New Post</a>
        </li>
      </ul>
      <ul class="navbar-nav ml-auto">
        <li class="nav-item">
          <a class="nav-link" href="https://www.linkedin.com/in/trey-cooper-8a231a123/" target="_blank"><i class="fa fa-linkedin fa-lg white-text fa-2x"></i></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="https://github.com/cooper6101" target="_blank"><i class="fa fa-github fa-2x"></i></a>
        </li>
      </ul>
    </div>
  </div>
</nav>
<div class="container-fluid siteBody">
  <div id="container">
    <h1>TO-DO List <i class="fa fa-plus"></i></h1>
    <input type="text" name="addTodo" placeholder="Add New Todo">
    <ul id="todoItems">
      <li class="todoItem"><span class="remove"><i class="fa fa-trash"></i></span> Grocery Shop</li>
      <li class="todoItem"><span class="remove"><i class="fa fa-trash"></i></span> Get coding job</li>
      <li class="todoItem"><span class="remove"><i class="fa fa-trash"></i></span> Finish both Bootcamps</li>
      <li class="todoItem"><span class="remove"><i class="fa fa-trash"></i></span> Walk Fish</li>
      <li class="todoItem"><span class="remove"><i class="fa fa-trash"></i></span> Cook Dinner</li>
      <li class="todoItem"><span class="remove"><i class="fa fa-trash"></i></span> Book a flight</li>
    </ul>
  </div>
</div>

【讨论】:

这更干净了,谢谢。不幸的是,它仍然导致同样的问题。我正在使用布局的快速应用程序中运行此代码。我想知道这是否会导致我的问题。

以上是关于jQuery id 选择器正在选择所有元素的主要内容,如果未能解决你的问题,请参考以下文章

jQuery选择器

jQuery 选择器

jquery选择器

jQuery 选择器与事件

jQuery选择器的总结

jQuery选择器