douxunwei7083 2012-11-20 13:44
浏览 25
已采纳

如果点击则突出显示三个段落中的一个(在其中取消选择其他段落)

I have three paragraphs which are made to look like three seperate buttons:

<p id="showCloud" class="newRefreshThree" name="tabbed"><span class="ico"></span><a href="">{i18n key="evadmin.title.cloud"}</a></p>

{if $enableCapacityReport=="1"}

<p id="showStats" name="tabbed"><span class="ico"></span><a href="" >{i18n key="evadmin.stats.showstats"}</a></p>

{/if}

<p id="showInfrastructure"  name="tabbed" class="newRefreshFour"><span class="ico"></span><a href="">{i18n key="evadmin.title.infrastructure"}</a></p>

All of them share the same name, name = tabbed

Each of these paragraphs/buttons send a user to a seperate tab and I would like to highlight the button of whichever tab is selected, while making sure all the other types are then un-highlighted.

Just now I wrote what I thought was the first part of the JQuery I need:

$('p[name=tabbed]').click(function(){
        $(this).css('background-color','red');
        });

but obviously this doesn't deselect the other p's, and I didn't know the most efficient/nicest way of doing it..

Also I would like to have an initial button/p highlighted when the page orignally loads so that it starts off highlighted for that tab.

Could anyone recommend to me what event/method I can use to do everything I just stated, or even give an example? Regards

  • 写回答

3条回答 默认 最新

  • dongqin1167 2012-11-20 13:47
    关注

    If you're not going to be dynamically adding <p> elements to the page, then you can cache the selector, use that to manipulate all of the paragraphs, then finally apply your style to the element that was clicked on.

    var $p = $('p[name="tabbed"]');
    $p.click(function(e) {
        $p.css('background-color', '');
        $(this).css('background-color', 'red');
    });
    

    Or, use a CSS class declaration, and use the removeClass() and addClass() functions to add/remove that class as appropriate:

    CSS .red { background-color: red; }

    jQuery:

    var $p = $('p[name="tabbed"]');
    $p.click(function(e) {
        $p.removeClass('red');
        $(this).addClass('red');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度