jquery parent /最近的不工作

标签 jquery parent closest

我有一个链接,可以将一些数据发布到 Controller 。它工作正常并更新数据。但我想更改父 div 的 css 但它没有发生。我尝试了很多变化,但我一定做了一些愚蠢的事情。

代码如下

查看:

@foreach (var item in Model)
{
    <div class="added-property-excerpt">
        ...
        <div class="added-property-links">
        ...
        @if (!item.IsPropertyDisabled) { 
                @Html.ActionLink("Take off the Market" , "Disable", "Property", new { id = item.PropertyId }, new { id ="disable-link" })
            }
            else {
                @Html.ActionLink("Bring on the Market" , "Enable", "Property", new { id = item.PropertyId }, new { id ="enable-link" })
            }
        </div>
    </div>
}

JQuery

 <script>
     $(function () {
         $('a#disable-link').click(function (e) {
             $('.added-property-links').text('loading...');
             $.ajax({
                 url: this.href,
                 dataType: "text json",
                 type: "POST",
                 data: {},
                 success: function (data, textStatus) { }
             });
             $(this).closest('.added-property-excerpt').css("background", "red");
          // $(this).parent('.added-property-excerpt').css("background", "red");
             e.preventDefault();
         });
     });
 </script>

最佳答案

你尝试过吗:

$(this).parents('.added-property-excerpt').css("background", "red");

parent s

它的作用是从一个父级到另一个父级,直到找到您想要的类。

关于jquery parent /最近的不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12878215/

相关文章:

javascript - 如何将每个带有 class 的 ul append 到其上方最接近的文章标签?

jQuery 选择字符串中的字符

javascript - 比较jquery中的相似字符串

c# - 在 MouseEvent 中获取成员的父类

jQuery:找到最靠近按钮的输入字段

python - 将 dict 键映射到 pandas 数据框的列(如果它们接近)

javascript - Jquery:读取具有名称作为关联数组的表单输入文本

javascript - 使用 jquery 更改元素类型

mysql - MS Access 2010 基于父字段过滤可重复使用的子表单

javascript - 在翻转时切换父元素的类