【问题标题】:jQuery: Button toggle isn't functioning as intendedjQuery:按钮切换未按预期运行
【发布时间】:2014-07-02 12:54:26
【问题描述】:

我现在正在学习 jQuery,想知道是否有可能使我的代码工作的方法?当我单击绿色按钮时,它不显示第二个表格行。我能做些什么来避免将来出现这个错误?我愿意接受任何帮助!请,谢谢。

jquery:

$(document).ready(function () {
    $("button").click(function () {
        $(".row2").show();
    });
});

html:

<table>
    <tr class="row1">
        <td>
            <p>This is where the text in the boxes will go!</p>
        </td>
        <td>
            <p>This is where the text in the boxes will go!</p>
        </td>
        <td>
            <p>This is where the text in the boxes will go!</p>
        </td>
        <tr class="row2">
            <td>
                <p>This is where the text in the boxes will go!</p>
            </td>
            <td>
                <p>This is where the text in the boxes will go!</p>
            </td>
            <td>
                <p>This is where the text in the boxes will go!</p>
            </td>
        </tr>
</table>
<a class="greenbuttontext" href="#"><button class="showmorebutton">Show9 more concepts</button></a>

css:

/*styles for green showmore button*/
 .showmorebutton {
    height: 33px;
    border: solid #628b06 1px;
    border-radius: 5px;
    width: 200px;
    margin-left: 600px;
    margin-bottom: 25px;
    text-align: center;
    background-color: #99cd00;
    color: white;
    vertical-align: center;
    line-height: 33px;
}

.greenbuttontext {
    text-decoration: none;
}

/*row2 disappear*/
.row2 {
    display:none;
}

【问题讨论】:

  • 工作正常jsfiddle.net/uk3E3/9。可能你没有在代码中引用 jquery
  • 控制台有错误吗?你是否至少正确地包含了 jQuery???
  • 我最终一起删除了锚标记,并且它起作用了。谢谢!

标签: jquery html css button toggle


【解决方案1】:

您的 html 无效。标签内的按钮

<a class="greenbuttontext" href="#"><button class="showmorebutton">Show
            9 more concepts</button></a>

改成

<a class="greenbuttontext" href="#"></a>
<button class="showmorebutton">Show
            9 more concepts</button>

注意:标签是内联块元素。

【讨论】:

  • 谢谢!这工作100%!我最终只是一起摆脱了锚标签。
【解决方案2】:

不要将&lt;button /&gt; 包装在&lt;a /&gt; 标记中?点击事件可能在锚点而不是按钮上触发。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多