简体   繁体   中英

Select a tag from different form

I have two forms and each form has different a tag , but when I use Javascript to use a tag different form, I can work on the a tag from the first form . How can make Javascript so that I can work on different a ta g from different forms. My codes are shown below.

Form to sell
<body>
<form  class="summarybackground" name="sell"  id="sell" style="height:500px;width:920px;overflow-y:hidden;"  method="post">

<div class="myBox"> 
<nav id="cd-vertical-nav">
        <ul>
            <li>
                <a data-number="1" href="#section1" class="is-selected">
                    <span class="cd-dot"></span>
                    <span class="cd-label">Landed</span>
                </a>
            </li>           

        </ul>
</nav>
<div class="col-sm-9">
      <div id="section1">    
        <h1 class="header double">Landed</h1>
      </div> 
</div>
</form>

Form to rent

<form  class="summarybackground" name="rent"  id="rent" style="height:500px;width:920px;overflow-y:hidden;"  method="post">

<div class="myBox"> 
<nav id="cd-vertical-nav-sec">
        <ul>
            <li>
                <a data-number="1" href="#section1" class="is-selected">
                    <span class="cd-dot"></span>
                    <span class="cd-label">Landed</span>
                </a>
            </li>           

        </ul>
</nav>
<div class="col-sm-9">
      <div id="section1">    
        <h1 class="header double">Landed</h1>
      </div> 
</div>
</form>
</body>

This script can work on the first form's a tag only.

<script>
$('a').click(function () {
    $('a').removeClass('is-selected');
    $(this).addClass('is-selected');
});
</script>

How to make a script so that I can work on both forms?

Find your a using it's parent

<script>
$('#rent a').click(function () {
    $('#rent a').removeClass('is-selected');
    $(this).addClass('is-selected');
});
</script>

You can do similar thing for your next form

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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