c# - Onclick 不是这个
问题描述
我为侧边栏创建了一个功能,当单击它时,它会向下滑动第一个 href,然后任何不是它的东西都会向上滑动。这工作正常,直到我需要使用“onclick”调用该函数,这意味着 $(this) 不能正常工作,因为它只能调用 (this) 。我正在使用标签生成器,因此只能调用一个唯一的事件侦听器。
JS
$(".foo > a").each(function () {
$(this).click(function () {
$(this).siblings(".sibling").slideToggle();
$(this).toggleClass("active");
$(".foo > a").not(this).siblings(".sibling").slideUp();
$(".foo > a").not(this).removeClass("active");
});
});
C#
a.MergeAttribute("onclick", "Click.call(this)");
不是这个以侧边栏为目标,也不是向上滑动
解决方案
要不将 CSS 类添加到当前目标,只需删除当前 CSS 并添加到其兄弟姐妹。
$(".foo > a").click(function () {
$(this)
.removeClass('active')
.siblings()
.addClass('active');
});
.foo {
display: flex;
}
.foo a {
border: 1px solid black;
color: black;
padding: 15px;
margin: 15px;
text-decoration: none;
}
.foo a.active {
background: black;
color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="foo">
<a href="#">Link1</a>
<a href="#">Link2</a>
<a href="#">Link3</a>
</div>
推荐阅读
- html - 什么机制阻止 div 完全占据其父级的宽度?
- mysql - 为什么“如果存在 konami 则删除程序”在 mysql 中不起作用?
- java - 为什么在我的 IDE 中找不到 javafx.mail.jar Java 库?
- c# - 得到错误:ImportError: cannot import name input_reader_pb2 when start tensorflow training in process C#
- ajax - 无法在 Laravel 中使用 AJAX 发布接收数据
- scala - 在 pyspark 的 Scala UDF 中使用默认参数值?
- maven - Maven 程序集 - 单独的 jar 和依赖项
- javascript - 如何在 WordPress 中将 $wpdb->get_results 作为 json 返回?
- docker - dockerfile - 无法在 /var/log 中创建文件
- javascript - 在传递给父页面的 iframe 上滚动但未触发轮事件?