社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Taplar

Taplar 最近创建的主题
Taplar 最近回复了
6 年前
回复了 Taplar 创建的主题 » Jquery-根据表行类更改表单元格值

你的html有一个错别字,但是其他的。。。

$('.tblServices > tbody > tr.loc2').each(function(){
  $('td.tcDayTimes', this).html("24 hours")
});

在哪里? this 是您正在迭代的tr。第二个论点 $()

或者

$('.tblServices > tbody > tr.loc2 td.tcDayTimes').html("24 hours");

为什么使用委托事件侦听器?

1)内容尚不存在

//#container is empty, but we will create children in the future
//we can use a delagate now that will handle the events from the children
//created later
$('#container').on('click', '.action', function (e) {
  console.log(e.target.innerText);
});

//lets create a new action that didn't exist before the binding
$('#container').append('<button class="action">Hey!  You Caught Me!</button>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container"></div>

2)内容存在,但有变化

//#container has an existing child, but it only matches one of our
//delegate event bindings.  Lets see what happens when we change it
//so that it matches each in turn

$('#container').on('click', '.action:not(.active)', function (e) {
  console.log('Awww, your not active');
  $(e.target).addClass('active');
});

$('#container').on('click', '.action.active', function (e) {
  console.log('Hell yeah!  Active!');
  $(e.target).removeClass('active');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
  <button class="action">Hey! You Caught Me!</button>
</div>

为什么在委托事件侦听器上使用非委托事件侦听器?

1)内容是静态的和预先存在的

要么是因为你知道内容是静态的,不会改变,你不需要一个。否则,您可能有使用委托的首选项,这与开发人员首选项一样好。

2)可以防止事件冒泡

但是,使用非委托事件侦听器也可以与委托一起使用,以防止操作。请考虑以下几点:

//#container has three children.  Lets say we have a delegate listener for
//the buttons, but we only want it to work for two of them.  How could we
//use a non-delegate to make this work?

//delegate that targets all the buttons in the container
$('#container').on('click', 'button', function (e) {
  console.log('Yeah!');
});

$('.doNotDoSomething').on('click', function (e) {
  console.log('Do not do the delegate logic');
  
  //by stopping the propagation of the click event, it will not bubble up
  //the DOM for the delegate event handler to process it.  In this way, we
  //can prevent a delegate event handler from working for a nested child.
  e.stopPropagation();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
  <button class="doSomething">Do It!</button>
  <button class="doNotDoSomething">Nooooo!</button>
  <button class="doSomethingElse">Do This Instead!</button>
</div>

3)你希望你的绑定是可移动的

可能希望使用非委托事件侦听器的另一个原因是 依附于元素本身。因此,如果删除元素,绑定也会随之消失。虽然这可能是动态内容的一个问题,您希望元素的绑定始终存在,但在某些情况下,您可能希望这样做。

7 年前
回复了 Taplar 创建的主题 » jquery样式未应用于其元素

你必须瞄准 next() 你想展示的。

//get a reference to all the links
var $dropdownLinks = $('.dropdown-link');

//bind the click handler to the links.
$dropdownLinks.on('click', function() {
  var $this = $(this);

  //remove the active class from the other links
  $dropdownLinks.not(this).removeClass('active');
  //toggle the active class on this element so it can open or close
  $this.toggleClass('active');
});
//make the link red if active
.dropdown-link.active {
  color: red;
}

//hide the immediately following sibling dropdown-list element
//of the dropdown-link, so long as it is not active
.dropdown-link:not(.active) + .dropdown-list {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <div class="logo">LOGO</div>
  <ul>
    <li><a href="#" class="active dropdown-link">Home</a></li>
    <li><a id="d1" class="dropdown-link" href="#">Projects</a>
      <ul class="dropdown-list" onclick="myFunction(this)">
        <li><a style="line-height: 0;" href="#">Pictures</a></li>
        <li><a style="line-height: 0;" href="#">Movies</a></li>
        <li><a style="line-height: 0;" href="#">Slow-mo</a></li>
      </ul>
    </li>
    <li><a id="d2" class="dropdown-link" href="#">About me</a>
      <ul class="dropdown-list">
        <li><a style="line-height: 0;" href="#">CV</a></li>
        <li><a style="line-height: 0;" href="#">Phone</a></li>
      </ul>
    </li>
    <li><a href="#" class="dropdown-link">Contact</a></li>
  </ul>
</nav>