简体   繁体   English

如何在javascript中将相关的HTML元素绑定在一起?

[英]How to bind related HTML elements together in javascript?

I am a newbie to HTML/Javascript so pardon me if the question is very simplistic. 我是HTML / Javascript的新手,如果问题很简单,请原谅。 I am trying to develop a toy HTML app to maintain a todo list. 我正在尝试开发一个玩具HTML应用程序以维护待办事项列表。 I am not really stuck but would like to hear about some good design decisions for the problem below. 我并不是真的很固执,但是想听听有关以下问题的一些好的设计决策。

I have a django server which serves me a list of elements in the following form: 我有一个django服务器,它以以下形式为我提供元素列表:

<ul>
    <li  id="todo_1">Get groceries <a class="deletelink" href="/delete/1/">[X]</a></li>
    <li  id="todo_2">Water plants <a class="deletelink" href="/delete/2/">[X]</a></li>
    <li  id="todo_3">Repair bike <a class="deletelink" href="/delete/3/">[X]</a></li>
</ul>

I also have a bit of jQuery that is called whenever one of the "[X]" links is clicked, which deletes the parent list element like this: 每当单击“ [X]”链接之一时,我也会有一点jQuery,它会删除父列表元素,如下所示:

$(".deletelink").on("click", function (event) {
    event.preventDefault();
    var parentItem = $(this).parent(); // the problem is here
    $.ajax({ 
    ... // ajax post request to server here
    }).success( function (status, data) {
        parentItem.delete();
    });
});

Now, the problem is that if I change the structure of the HTML a bit, eg doing <div><a class="deletelink" href="/delete/1/">[X]</a></div> , I have to go and change the javascript as well. 现在的问题是,如果我稍微改变HTML的结构,例如做<div><a class="deletelink" href="/delete/1/">[X]</a></div> ,我也必须去更改javascript。 So, I figured out another way to do this. 因此,我想出了另一种方法。 Now, each element has its own id as well, like: 现在,每个元素也都有自己的ID,例如:

    <li  id="todo_1">Get groceries <a class="deletelink" id="deletelink_1" href="/delete/1/">[X]</a></li>

Upon click, the javascript changes to 单击后,javascript更改为

var parentItem = $("#todo_" + (/\D+_(\d+)/.exec(this.id)[1]));

So this implicitly binds each deletelink_i to the todo_i list item. 因此,这将每个deletelink_i隐式绑定到todo_i列表项。 However this looks very hacky (eg, now if I have a list within another list, I can't easily get all the items within the first list). 但是,这看起来很hacky(例如,现在如果我在另一个列表中有一个列表,那么我将无法轻易获得第一个列表中的所有项)。 My question is that is there a better or easier way to do all this in javascript? 我的问题是,有没有更好或更简单的方法可以在javascript中完成所有这些操作? If possible, I would really like to keep the HTML rendering on the server side. 如果可能的话,我真的很想将HTML呈现保留在服务器端。

您正在试图删除最接近li的祖先,所以你可以使用.closest()找到所需的祖先

var parentItem = $(this).closest('li');

I'd say this is a nice example of how usefull the data attribute is. 我想说这是数据属性多么有用的一个很好的例子。

This makes the strukture as flexible as possible, you could just do 这样可以使结构尽可能灵活,您可以这样做

<li  id="todo_1" data-item-id="1">
    Get groceries 
    <a class="deletelink" data-item-id="1" href="#">
        [X]
    </a>
</li>

And this is the jQuery Part 这是jQuery部分

$(".deletelink").on("click", function (event) {
    event.preventDefault();
    var selectedItem = $(this).attr('data-item-id')
    $.ajax({ 
    ... // ajax post request to server here
    }).success( function (status, data) {
        $('[data-item-id="'+selectedItem+'"]').remove();
    });
});

parent()接受选择器,因此您可以选择仅查看作为li元素的父元素:

var parentItem = $(this).parents('li');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM