javascript - 在 <a> 内切换 span

标签 javascript jquery html

我正在尝试创建一个函数,如果选择的话,它将切换可选输入,这是我到目前为止所做的:

HTML:

<div>
    <input>
    <a class="input-toggle" href="#"><span>Toggle Option</span><span>Close</span></a>
    <div class="input-toggle-content">
        <input name="">
    </div>
</div>

JavaScript:

$('.input-toggle').each(function() {
    $(this).next("div").hide().end();
    $("span:last-of-type").hide();
    $(this).on('click', function() {
        $(this).next("div").slideToggle();
        $("span:first-of-type").hide();
        $("span:last-of-type").show();
    });
});

所以,它应该工作的方式是,当单击 .input-toggle 时,它​​旁边的 div 将被切换,如果再次单击,div 将消失......我得到了这个工作,但是,我还想切换<span>Toggle Option</span><span>Close</span>我无法让它工作...我不知道我构建函数的方式是否正确?

最佳答案

尝试,

$('.input-toggle + div.input-toggle-content').hide();
$(".input-toggle span:last-of-type").hide();

$('.input-toggle').click(function () {
    $(this).next('div.input-toggle-content').toggle();
    var spans = $('span', this);
    spans.not(spans.filter(':visible').hide()).show();
});

DEMO

关于javascript - 在 <a> 内切换 span,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24474416/

相关文章:

javascript - SAPUI5 在 Controller 中动态创建 ComboBox

javascript - 用页面加载百分比填充透明 Logo

javascript - 需要帮助编写 js 函数来更改调用它的按钮的类

html - 表行跨度单元格跨度

javascript - 包装组件意味着它失去了对重新渲染的关注

javascript - 在 JavaScript 中仅使用 `.reduce()` 交换数组中的 2 个元素?

javascript - 在文档的任何地方更改类 onclick 以关闭菜单?

javascript - 如何编写供本地使用的 jquery 可链接函数?

html - 如何对 antd-table 进行排序,同时永远在表格顶部保留一个特殊行?

javascript - 使用 attr() 和数据标签操作 css