javascript - 上下滑动目标div不起作用

标签 javascript jquery html

晚上好,

我正在尝试使目标 div 上下滑动(如可见和隐藏)。

但是,当指向该函数的链接不在同一行时(例如在同一个 <p> 中),它们将不起作用。只是第一个出现,然后就不起作用了......什么也没有发生。 另外,当我单击打开的 <div> 的链接时,它应该隐藏。再次。但事实并非如此……

有代码,我不知道为什么它不能正常工作...... 谢谢您的帮助!

JS:

    <script>
jQuery(function () {
    jQuery('.showSingle').click(function () {
        var index = $(this).index(),
            newTarget = jQuery('.targetDiv').eq(index).slideDown();
        jQuery('.targetDiv').not(newTarget).slideUp();

    });
});
 </script> 

HTML(链接的无效版本):

<table width="80%" border="0" align="center">
<tr width="100%"><td width="35%">
 <p align="center" style="font-size: 25px; color: ;"><a class="showSingle" target="1">Životní krizové situace<br />
 <span style="font-size:16px;">„Dovolte mi pracovat s Vámi, když jste v krizi.“</a></p>
</td>

<td width="30%">&nbsp;</td>

<td width="35%">
 <p align="center" style="font-size: 25px; color: ;"><a class="showSingle" target="2">Relaxace, meditace, imaginace<br />
 <span style="font-size:16px;">„Zastavte se a načerpejte sílu.“</a></p>
</td>
</tr> 

 </td>
</tr>
</table>

HTML(工作环版本的链接):

<p align="center" style="font-size: 22px; color: white; line-height: 55px;"><a class="showSingle" target="1">VIP Inkaso</a>
&nbsp;&nbsp;
<a class="showSingle" target="2">Vystěhování nájemníků</a>
&nbsp;&nbsp;
<a class="showSingle" target="3">Vymáhání pohledávek</a></p>

HTML(目标 div):

<div id="div1" class="targetDiv">
$text
</div>

<div id="div2" class="targetDiv">
text
</div>

CSS:

   .targetDiv {
    display: none
}

最佳答案

你可以试试这个:

jQuery(function () {
    jQuery('.showSingle').click(function () {
        var index = jQuery('.showSingle').index($(this));
        jQuery('.targetDiv').eq(index).slideToggle().siblings('.targetDiv').slideUp();
       //up here, toggle the selected and closes the remaining

    });
});

在这种情况下,该函数还会上下移动元素 .targetDiv

关于javascript - 上下滑动目标div不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34757029/

相关文章:

javascript - Angularjs ng-repeat、ng-form 和验证错误

php变量知道jquery何时加载?

javascript - Dynamics Nav Control 插件支持 Internet Explorer

html - 即使其他浏览器不需要,CSS3PIE .htc 文件也会加载吗?

javascript - 如何检查单选按钮是否已经与 "change"事件处理程序绑定(bind)

Javascript - window.open 'location=no' 仍然在 Firefox 中显示地址栏

javascript - 图像未加载并插入数组(javascript)

javascript - 压缩 JavaScript 文件

javascript - 如何从网站获取苹果触摸图标,如果不存在则显示图标?

javascript - 如何使用 html、css 和 js 添加自定义动画弹出按钮?