jquery - 无法在 HTML/CSS 中设置事件按钮

标签 jquery html css

我对使用 HTML 和 CSS 非常陌生,并且在让按钮显示它们处于事件状态时遇到了一些麻烦(即默认情况下,button1 具有替代颜色以显示其已被选中)。当他们点击 button2 时,上面的内容发生变化(成功),然后 button2 应该成为事件的。

但是,目前,我什至无法让 button1 成为默认事件状态。我已在页面的其他地方成功完成此操作。

这是 HTML:值得注意的是我在这个类中的另一个成功尝试也命名为“active”

<div id="left-top-container">
        <ul id="selectionInfo">                    
            <li id="disc">Noiseproofing Joist Tape</li>
            <li id="compound">Compound</li>
            <li id="clip">Clip</li>
            <li id="sealant">Sealant</li>                    
        </ul>
    </div>
    <div id="left-bottom-container">                
        <ul id="buttons">
            <li id="discSelection" class="active"><a href="#disc">Disc</a></li>
            <li id="compoundSelection"><a href="#compound">Compound</a></li>
            <li id="clipSelection"><a href="#clip">Clip</a></li>
            <li id="sealantSelection"><a href="#sealant">Sealant</a></li>
        </ul>
    </div>

这是 CSS:

#buttons
{ position:relative; left:-44px; top:-44px; }
#buttons li
{ float:left; list-style:none; }
#buttons li a
{ float:left; list-style:none; position:relative; text-indent:-9999px; }
#discSelection a
{ background: url(/Content/images/unhighlighted.png) no-repeat 0 0; height:88px; width:162px; padding:0; }
#discSelection a:hover, #discSelection a.active
{ background: url(/Content/images/tops.png) no-repeat 0 0; width:162px; height:106px; top:-13px; }
#compoundSelection a
{ background: url(/Content/images/unhighlighted.png) no-repeat -162px 0; height:88px; width:159px; padding:0; }
#compoundSelection a:hover, #compoundSelection a.active 
{ background: url(/Content/images/tops.png) no-repeat -162px 0; width:159px; height:106px; top:-13px; } 
#clipSelection a
{ background: url(/Content/images/unhighlighted.png) no-repeat -321px 0; height:88px; width:159px;  }
#clipSelection a:hover, #clipSelection a.active
{ background: url(/Content/images/tops.png) no-repeat -324px 0; width:159px; height:106px; top:-13px; }
#sealantSelection a
{ background: url(/Content/images/unhighlighted.png) no-repeat -480px 0; height:88px; width:159px; }
#sealantSelection a:hover, #sealantSelection a.active
{ background: url(/Content/images/tops.png) no-repeat -486px 0; width:159px; height:106px; top:-13px; }   

这是 JQuery 脚本:

$(document).ready(function () {
$("#main-body-options a").click(function (e) {
    showSlide($(this).parent().index())
    return false;
});
$("#buttons a").click(function (e) {
    showSlide2($(this).parent().index())
    return false;
});
});

function showSlide2(index) {
$("#buttons.active li.active").removeClass("active")
$("#buttons li").eq(index).addClass("active")
$("ul#selectionInfo").animate({ top: -409 * index}, { duration:600, easing:"easeInOutQuint" })
}

如果我在任何地方都不清楚,请询问或编辑以使其更具可读性。

最佳答案

检查所有的 css 和 html 并将事件类仅放在 a 元素上

然后将你的 jquery 修改为:

$("#buttons a").click(function (e) {
   $("#buttons a").removeClass("active");
   $(this).addClass("active");
   showSlide2($(this).parent().index())
return false;
});

 function showSlide2(index) {
    $("ul#selectionInfo").animate({ top: -409 * index}, { duration:600, easing:"easeInOutQuint" })
 }

关于jquery - 无法在 HTML/CSS 中设置事件按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9484338/

相关文章:

jQuery Masonry Mobile 2 列布局修复

jquery - 为什么 jQuery SlideUp() 中的回调不等待动画完成?

php - 如何使用 css 和 jquery 隐藏具有数据库值的表?

javascript - 奇怪的 html 渲染异常。

javascript - 如何在点击链接时在 meteor 中显示图标

javascript - 用于使 div 与页面/文档大小相同的 CSS/javascript

css - 标签的点击获取里面的复选框值

jquery - 交换两个 jQuery 可拖动列表项无法正常工作(以 jsFiddle 为例)

javascript - jquery - 使用 .done()、.then() 和 .when() 按给定顺序发出 ajax 请求

javascript - 将 javascript 变量值传递给输入类型隐藏值