jquery - toggleClass() 不适用于 div

标签 jquery html css

我在网站上工作时遇到了问题。在我的常见问题解答中,我有一个下拉菜单和一个提交按钮,用于链接到常见问题解答的不同部分。这非常有效。提交按钮触发一个 javascript 函数,该函数动态链接到页面上的适当 anchor 。此外,我希望通过下拉菜单选择的问题以橙色突出显示。我得到了要显示的高光,但只是一瞬间就闪走了。我想知道如何让高光停留,而不是闪走?我不确定为什么会这样。另外,如何使用 jQuery 使高光淡出?我知道如何通过隐藏 div 然后使用 .fade() 来做到这一点,但这会导致所有文本消失。非常感谢!

这是一个 fiddle :http://jsfiddle.net/UjPtv/但它不能完全工作,因为当您提交时会出现错误。我认为这是因为 jsfiddle 不允许提交,即使这个提交只是运行一些 javascript。如果您知道解决方法,请告诉我,以便我更新链接!谢谢!

JavaScript:

var toggled = 0;
function jump_to (location) {
if(toggled == 0){
    toggled = 1;
    window.location.href = "#" + location;
    $("#wrap"+location).toggleClass("wraps_active");
}
}​

html:

<div id = "main">
        <h2 class = "center">FACTS YOU NEED</h2>
        <h3>The Core Facts You Want To Know</h3>
        <div class = "border">
        <p>
            Jump To: 
              <form onsubmit = "jump_to(jump_list.value)">
              <select name = "jump_list">
              <option value="1">Who can go to the camp?</option>
              <option value="2"><a href = "contact_information.html">When do we check in and out?</a></option>
            </select>
            <input type = "submit" value = "Go"/>
            </form>
        </p>
        </div>
        <div id = "wrap1" class = "wraps">
        <h4><a name = "1"></a>Who can go to the camp? </h4>
        <p>
            The camp is for kids ages 9 to 17 years old who have been diagnosed with celiac disease, a condition that requires life-long adherence to a gluten-free diet.  Given limited space, we are unable to accommodate kids on gluten-free diets for other reasons (such as autism spectrum disorders).  Campers ages 16-17 years may choose to volunteer as junior counselors.  Junior counselors assist the adult volunteers, and for first session junior counselors need to arrive a day early (on Monday) for the staff orientation and training.  If there are more junior counselor applicants than there is available space, priority is given based on age, gender-need, and prior camp experience.
        </p>
        </div>
        <div id = "wrap2" class = "wraps">
        <h4><a name = "2"></a>When do we check in and out?</h4>
        <p>
            Check-in: Please see the "Calendar" on the left hand side of the website for details about each camp.Please do not arrive early.
            </br><br/>
            Check-out:  All campers must be checked out by the stated time so camp can be prepared for the next group, see the "Calendar" for date information.
        </p>
        </div>


    </div>​

CSS:

.wraps{
border-bottom:1px dashed #C3C3C3;
}
.wraps_active{
border-bottom:1px dashed #C3C3C3;
background:#FFB280;
}​

最佳答案

每次提交表单时,清除所有事件的包装类并仅为选定的包装类添加它。

$('form input[type=submit]').click(function(e) {
    e.preventDefault();

    var location = $('select[name=jump_list]').val();
    window.location.href = "#" + location;

    $('.wraps').removeClass('wraps_active');
    $("#wrap"+location).addClass("wraps_active");
});

这里是 Fiddle .

关于jquery - toggleClass() 不适用于 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14118080/

相关文章:

jquery - 将备用 CSS 应用于 jQueryUI 对话框?

php - 下拉菜单中的空白

html - 为什么 "script"需要结束标记而 "meta"不需要?

javascript - 如何使用 CSS 或 jquery 使链接不可点击?

html - 推特 Bootstrap : nested rows - border pushes span column down

jquery 表单插件 noConflict 问题

javascript - 使用 jquery 对话框在 jstree 中创建一个新节点

jquery - 在 Bootstrap 多选时调用 $(this)

javascript - $(this).attr ("id") 不工作

html - CSS 未在 iPhone 上加载