javascript - 更改切换 jquery 顶部的文本

标签 javascript jquery html

我有一堆这样的东西,当用户点击最大化时,div 展开以显示内容。内容向下切换后,底部的最小化链接可见。单击该链接时,它将正确切换,我只是在寻找一种方法来更改第一个链接中的文本,因为目前 jQuery 仅更改单击的链接的文本。有没有办法轻松做到这一点?使用 Parent() 然后使用 Child() 返回顶部表的东西 a?

切换时,应将 both #bridge 分别更改为 + Expand- Minimize

JavaScript

<script type="text/JavaScript" language="javascript">
    $(document).ready(function() {
        $(".expand").click(function(e) {
            e.preventDefault();
            var toggle = $(this).attr('href');

            var txt = $(this).text() == '+ Expand' ? '- Minimize' : '+ Expand';
            $(this).text(txt);
            $(toggle).slideToggle('fast', function() {                

            });

        });
    });
</script>

HTML

<!--Drop Down -->
    <div>
        <b class='bdrround'>
        <b class='bdrround1'><b></b></b>
        <b class='bdrround2'><b></b></b>
        <b class='bdrround3'></b>
        <b class='bdrround4'></b>
        <b class='bdrround5'></b></b>
        <div class='bdrroundfg'>
            <table width='100%' class='pagehead'>
                <tr>
                    <td width='80%' class='pagehead'>
                    <h2><a name='bureau'></a>Approved Bridging Statements (Optional):</h2>
                </td>
                <td width='20%' class='pagehead'>                    
                        <p align='right'><a href='#bridge' id="expand" class='expand'>+ Expand</a></p>                           
                </td>
                </tr>
             </table>
            <div id='bridge' class='faqcontainer' style='display:none;'>
                <p>
                    <b>Operational Direction: </b>These should be used to bring the customer back into the conversation and 
                    to transition into FAQs, Resolving Objections and overall general conversational flow.  
                    These statements are designed to let the customer know that we are listening, that his/her opinion is 
                    important and that we realize the value of that opinion.
                </p>
                <p>
                    <b>BRIDGING STATEMENTS:</b>
                    <ul>
                        <li>Now with that in mind &hellip;</li>
                        <li>That's an excellent question&hellip;</li>
                        <li>I hear what you are saying and&hellip;</li>
                        <li>I can (certainly) understand that and&hellip;</li>
                        <li>That's a very good point&hellip;</li>
                        <li>Please keep in mind&hellip;</li>
                        <li>I can understand your hesitation, but&hellip;</li>
                        <li>I can appreciate that&hellip;</li>
                        <li>Thank you </li>
                        <li>Perfect </li>
                        <li>Great </li>
                        <li>One moment please </li>
                        <li>Excellent</li>
                        <li>That’s great, now&hellip;</li>
                        <li>That’s correct</li>
                        <li>Let me clarify that for you</li>
                    </ul>
                    <span class="note">Note to Rep: </span>Use of Mr. or Ms. can be added throughout the call as appropriate 
                        to help build rapport with the customer.
                </p>
                <p>
                    <span class="note">[Note to Rep: Ensure all service needs are satisfied before offering.]</span>
                </p>
                <p>
                    [<span class="note">Directional:</span> If during the servicing of the call the customer mentioned 
                    they were retired, unemployed, or working less than 30 hours per week, go to: 
                    <a href="#">PS Basic Counter Offer Transition</a>]
                </p>
                <p align='right'><a href="#bridge" id="A2" class='expand'>- Minimize</a></p>
            </div>
            <b class='bdrround'>
            <b class='bdrround5'></b>
            <b class='bdrround4'></b>
            <b class='bdrround3'></b>
            <b class='bdrround2'><b></b></b>
            <b class='bdrround1'><b></b></b></b>
        </div>
        <div class='hrSectDiv'></div>
    </div>

最佳答案

不要使用 #some-id href 来切换你的元素,而是放入一个可重复使用的 var 所有按钮的共同父级

jsBin demo

  1. 添加类(class) .slide_content到你所有的可滑动元素( .faqcontainer )(或者只使用 .faqcontainer 而不是提到的,我不确定它是否是一个通用类)

  2. 完成后,我们必须找到哪些父元素 与以下元素相同: - 按钮和 - 使用 .closest() 的一个可滑动内容... 伟大的!你有一个!这是<div class='bdrroundfg'> !!!
    让我们在变量中使用他:var common_parent = $(this).closest('.bdrroundfg');

现在我们只需让 jQ 检索该元素 - 他的子按钮和他的可滑动内容:

    var slide_content = common_parent.find('.slide_content'); // the added class!
    var all_buttons   = common_parent.find('a.expand');       // both buttons

点击我们需要的是切换我们第一个(.eq(0))按钮的文本

     var txt = $(this).text() === '+ Expand' ? '- Minimize' : '+ Expand';
     all_buttons.eq(0).text(txt); // target only the first one in the collection

您的代码应该如下所示:

 $(".expand").click(function(e) {
     e.preventDefault();
     var common_parent = $(this).closest('.bdrroundfg');
     var slide_content = common_parent.find('.slide_content'); // the added class!
     var all_buttons   = common_parent.find('a.expand');   
         
     var txt = $(this).text() === '+ Expand' ? '- Minimize' : '+ Expand';
     all_buttons.eq(0).text(txt);    // toggle text - first button only!     
 
     $(slide_content).slideToggle('fast', function(){
          // do something here...
     });
 
 });

关于javascript - 更改切换 jquery 顶部的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11513872/

相关文章:

html - 在水平行中的网页框

javascript - 如何制作可折叠菜单 jQuery Mobile

javascript - 取消之前的 promise

javascript - 创建混合高度合理的标题?

jquery - 如何在 Jquery 中使用换行符从多行文本框中选择文本?

javascript - 确认 JavaScript 后运行 php 代码

php - 图像替换

javascript - 在 html 中引用 jquery 全局变量?

javascript - 使用 React 动态加载样式表

javascript - 将值传递给同一页面上的颜色框