简体   繁体   English

如何使用javascript单击链接有效地替换div内容?

[英]How to replace div contents effectively on clicking links using javascript?

I have created an HTML page that uses bootstrap CSS. 我创建了一个使用引导CSS的HTML页面。 I have a sidebar list which is actually a list of links. 我有一个侧边栏列表,实际上是链接列表。

<a href="#ut" class="list-group-item" id="utID">Update table</a>  

I have a number of sections in the HTML which is hidden by default by applying CSS like below. 我在HTML中有很多部分,默认情况下,通过应用如下所示的CSS隐藏这些部分。

<section id="ut" style="display:none;">

I want to change the content when i click the links in the sidebar. 单击侧边栏中的链接时,我想更改内容。 What I've used here is JavaScript that sets the CSS display of the clicked link's section to block. 我在这里使用的是JavaScript,它将点击链接部分的CSS显示设置为阻止。 For easier manipulation I remove the "ID" part from id of the link to get the id of the section Eg: link=utID , section= ut 为了便于操作,我从链接的ID中删除了“ ID”部分,以获取该部分的ID,例如:link = utID,section = ut

Given below is the JavaScript that I've used. 下面给出的是我使用过的JavaScript。 Is there a better optimized method to do this? 有没有更好的优化方法来做到这一点?

// On clicking any of the side bar links
$('.list-group-item')
.click(function(event){

    // Preventing the default action which may mess up the view
    event.preventDefault();

    // Getting all the anchor ids
    var $a1 = document.getElementById("unfID");
    var $a2 = document.getElementById("uiID");
    var $a3 = document.getElementById("utID");

    // Getting all the section ids
    var $d1 = document.getElementById("unf");
    var $d2 = document.getElementById("ui");
    var $d3 = document.getElementById("ut");

    // Store the id of the clicked link
    var $clickedLink = $(this).attr('id');

    // Storing the id of the corresponding Div by slicing of "ID" from the link's last part
    var $clickedLinkDiv = $clickedLink.substring(0,$clickedLink.length-2);

    // Setting the selected link active
    SetLinkActive(document.getElementById($clickedLink))
    // Setting the corresponding section visible
    SectionVisibility(document.getElementById($clickedLinkDiv));

    // Method to set the visibility of the section
    function SectionVisibility(div){
        // first hides al section
        $d1.style.display = "none";
        $d2.style.display = "none";
        $d3.style.display = "none";

        // then displays only the selected section
        div.style.display = "block";
    }

    // Method to set the visibility of the section
    function SetLinkActive(div){
        // first deselect all links
        $a1.className = "list-group-item";
        $a2.className = "list-group-item";
        $a3.className = "list-group-item";

        // then applies selection to only the selected link
        div.className = "list-group-item active";
    }
 });

Using jquery is much easier! 使用jQuery要容易得多!

The example 这个例子

HTML HTML

<a href="#" class="list-group-item" id="unf">Update UNF</a>  
<a href="#" class="list-group-item" id="ui">Update UI</a>  
<a href="#" class="list-group-item" id="ut">Update UT</a>  

<section class="unf" style="display:none;">
    UNF SECTION
</section>

<section class="ut" style="display:none;">
    UI SECTION
</section>

<section class="ui" style="display:none;">
    UT SECTION
</section>

JAVASCRIPT JAVASCRIPT

// On clicking any of the side bar links
$('.list-group-item').click(function(event){

    // Preventing the default action which may mess up the view
    event.preventDefault();

    $('a.list-group-item').removeClass('active');
    $(this).addClass('active');
    $('section').css('display', 'none');
    $('section.' + $(this).attr('id')).css('display', '');

 });

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

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