javascript - 在 Jquery 中切换时如何更改前置文本?

标签 javascript jquery toggle prepend

我有一些使用 Jquery 切换功能显示/隐藏的内容。

我在每个标题前面添加了一个箭头,并希望在切换内容时将前面的箭头更改为向下箭头。

到目前为止的代码:

$(document).ready(function(){
    $('.show-hide-list h3 a').prepend('> ');//Prepend arrow
    $('.show-hide-list li .show-hide-list-content').hide();//Hide content
    $('.show-hide-list h3').click(function() {//Perform toggle when clicked
        $(this).next('.show-hide-list-content').toggle();
        //Need to change orientation of prepended arrow when toggled
        return false;
    });
});

html如下:

<ul class="show-hide-list">
<li>
       <h3><a href="#">Heading</a></h3>
   <div class="show-hide-list-content">
           Content to be toggled here.
       </div>
    </li>
 </ul>

切换后如何更改箭头方向?

最佳答案

将箭头放在 span 标记中,以便您可以轻松选择它。

$(document).ready(function(){
    $('.show-hide-list h3 a').prepend('<span>&gt;&nbsp;</span>');//Prepend arrow
    $('.show-hide-list li .show-hide-list-content').hide();//Hide content
    $('.show-hide-list h3').click(function() {//Perform toggle when clicked
        $(this).next('.show-hide-list-content').toggle();
        var arrow = $(this).find('span');

        if (arrow.text().indexOf('V') >= 0) {
            arrow.html('&gt;&nbsp;');  
        } else {
            arrow.html('V&nbsp;');
        }
        //Need to change orientation of prepended arrow when toggled
        return false;
    });
}); ​

http://jsfiddle.net/6DB7t/1/

关于javascript - 在 Jquery 中切换时如何更改前置文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10721723/

相关文章:

javascript - 对对象数组进行排序

javascript - 使用 jquery 访问 gridview 内的隐藏字段并更新它?

Javascript 替换方法在复选框 if/else 切换中不起作用

javascript - 在 PC 和移动浏览器中运行的隐藏和显示切换

javascript - 更改 React Native 中其他组件的状态

javascript - 显式设置函数的返回类型

IE7 中的 JavaScript 求模运算

javascript - 使用 find 触发具有自己 id 的类

javascript - 单击文本框时如何添加 DatePicker?

javascript - 向侧边栏添加点击功能,而不影响其切换功能