简体   繁体   English

jQuery文本在第一次单击时不切换

[英]jQuery text not toggling on first click

I'm trying to toggle text of a span whenever it's parent anchor tag is clicked. 每当尝试单击父锚标签时,我都试图切换范围文本。 Here is the HTML: 这是HTML:

<h2 class="panel-title">
    <a href="#collapse1">
        <span class="ui-hidden-accessible">expand</span>
    </a>
</h2>

while this is the javascript of the changeText function which is called on each click of the anchor tag 而这是changeText函数的javascript,每次点击锚标记都将调用它

function changeText() {
var taskDivClass = $('.panel-title a');
$(taskDivClass).each(
    function() {
        var $this =  $(this);
        if($this.hasClass('collapsed')) {
            var taskDivSpanText = $this.find('.ui-hidden-accessible');
            taskDivSpanText.html('expand');
        }
        else {
            var taskDivSpanText = $this.find('.ui-hidden-accessible');
            taskDivSpanText.html('collapse');
        }
    }
);
}

I'm using the each method because there are multiple such blocks in the page, and for each click event only that particular span should change. 我使用的是each方法,因为页面中有多个此类块,并且对于每个click事件,仅应更改特定范围。

The .collapsed class dynamically toggles on the anchor tag when user clicks on it. 当用户单击.collapsed类时,它会动态切换锚标记。

The issue is when the first time user clicks on the anchor tag, the text is not toggled. 问题是,当用户第一次单击定位标记时,不会切换文本。 On next click it does. 在下一步单击它。

I'm really not sure what I'm missing here. 我真的不确定在这里缺少什么。 If someone has a clue, please point it out. 如果有人有线索,请指出。

You do not need to loop - the original selector already applies to all the matching elements 您无需循环-原始选择器已应用于所有匹配元素

 $(function() { $('.panel-title a').on('click', function() { var $this = $(this); if ($this.hasClass('collapse')) { var taskDivSpanText = $this.find('.ui-hidden-accessible'); $this.removeClass('collapse'); taskDivSpanText.html('expand'); } else { var taskDivSpanText = $this.find('.ui-hidden-accessible'); taskDivSpanText.html('collapse'); $this.addClass('collapse'); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h2 class="panel-title"> <a href="#collapse1" class="collapse"> <span class="ui-hidden-accessible">collapse</span> </a> </h2> <h2 class="panel-title"> <a href="#collapse2"> <span class="ui-hidden-accessible">expand</span> </a> </h2> <h2 class="panel-title"> <a href="#collapse3"> <span class="ui-hidden-accessible">expand</span> </a> </h2> 

You could also encapsulate the function into a named function like in your code 您也可以像在代码中那样将该函数封装到一个命名函数中

 $(function() { // define function var changeText = function() { var $this = $(this); if ($this.hasClass('collapse')) { var taskDivSpanText = $this.find('.ui-hidden-accessible'); $this.removeClass('collapse'); taskDivSpanText.html('expand'); } else { var taskDivSpanText = $this.find('.ui-hidden-accessible'); taskDivSpanText.html('collapse'); $this.addClass('collapse'); } }; // bind function to click event $('.panel-title a').on('click', changeText ); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h2 class="panel-title"> <a href="#collapse1" class="collapse"> <span class="ui-hidden-accessible">collapse</span> </a> </h2> <h2 class="panel-title"> <a href="#collapse2"> <span class="ui-hidden-accessible">expand</span> </a> </h2> <h2 class="panel-title"> <a href="#collapse3"> <span class="ui-hidden-accessible">expand</span> </a> </h2> 

If you dont want to add/remove class work with .html(). 如果您不想使用.html()添加/删除类工作。

Hope this will help you.. 希望这个能对您有所帮助..

 $(document).ready(function(){ $("a").click(function(){ var spanClick= $(this).find('.ui-hidden-accessible'); if (spanClick.html()==="Expand"){ spanClick.html("Collapse"); } else{ spanClick.html("Expand"); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h2 class="panel-title"> <a href="#collapse1" > <span class="ui-hidden-accessible">Expand</span> </a> </h2> 

https://jsfiddle.net/7vx1ueo1/2/ https://jsfiddle.net/7vx1ueo1/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM