简体   繁体   English

如何动态添加 <li> jquery列表中的标签?

[英]How to add dynamically <li> tags in jquery list?

In the moment I can add li tags to my list with script. 目前我可以使用脚本将li标签添加到我的列表中。 But how can I add dynamically li tags in a function in .js? 但是如何在.js中的函数中动态添加li标签? Hopefully I will see a good example. 希望我能看到一个很好的例子。 Below is my code. 以下是我的代码。 Thanks! 谢谢!

<div data-role="page" id="searchPage" data-theme="b">
<div data-role="content">
    <ul data-role="listview" data-filter="true" data-theme="b" id="searchListUl">
    </ul>
</div>
<script type="text/javascript">
  $("#searchListUl").append('<li data-filtertext="Apple"><a href="#">Apple</a></li>');
  $("#searchListUl").listview('refresh');</script></div>

Your function would be something like: 你的功能将是这样的:

var addItem = function(item){
    $("#searchListUl").append('<li data-filtertext="'+item+'"><a href="#">'+item+'</a></li>');
}

You can call it with: addItem("apple") 您可以使用以下命令调用它: addItem("apple")

If you with "dynamic" mean that you want to be able to append list items without knowing the name ("Apple"), you could make a generic function, using the jQuery function used to create elements: 如果您使用“动态”意味着您希望能够在不知道名称(“Apple”)的情况下附加列表项,则可以使用用于创建元素的jQuery函数创建泛型函数:

function add(name) {
    var $li = $("<li>").attr("data-filtertext", name)
                       .appendTo("#searchListUI");

    $("<a>").attr("href", "#")
            .text(name)
            .appendTo($li);
}

You could use it as follows: 您可以按如下方式使用它:

<div data-role="page" id="searchPage" data-theme="b">
<div data-role="content">
    <ul data-role="listview" data-filter="true" data-theme="b" id="searchListUl">
    </ul>
</div>
<script type="text/javascript">
  add("Apple");
  $("#searchListUl").listview('refresh');</script></div>

It should be noted that using .append() in the way described in many of these answers will directly vulnerable to Cross Site Scripting (XSS) attacks. 应该注意的是,以许多这些答案中描述的方式使用.append()将直接容易受到跨站点脚本(XSS)攻击。 If strings are being pulled from a data source that can be influenced by users, then a user can put raw HTML, and when .append() is called, the raw HTML will get directly injected into the DOM. 如果从可能受用户影响的数据源中提取字符串,则用户可以放置原始HTML,并且当调用.append()时,原始HTML将直接注入DOM。 Script tags are especially dangerous. 脚本标签特别危险。 Once you can run arbitrary javascript in someones session, an attacker can grab their cookies, their private information, and even passwords in some circumstances, all without the victim knowing. 一旦你可以在某个人的会话中运行任意的javascript,攻击者就可以在某些情况下获取他们的cookie,他们的私人信息甚至密码,所有这些都没有受害者知道。

Always ALWAYS use .text() to set the text of an element. 始终始终使用.text()来设置元素的文本。 In jquery, .text() will properly HTMLEncode characters when it needs to. 在jquery中,.text()会在需要时正确HTMLEncode字符。 If you have experience with SQL, you can think of .append($RANDOM_STRING) the same way that you would think of conn.exec($RANDOM_STRING). 如果你有使用SQL的经验,你可以想象.append($ RANDOM_STRING)与你想到的conn.exec($ RANDOM_STRING)相同。 By their very nature, they are vulnerable to injection and should be avoided at all costs. 就其性质而言,它们易于注射,应不惜一切代价避免使用。

I'm sure code from these examples have already been copied and pasted, and are now in use all over the internet, and that sucks. 我确信这些示例中的代码已经被复制和粘贴,现在正在互联网上使用,这很糟糕。 Please get the word out, .append() will append HTML, not just text, so improper use will let attackers inject raw HTML into your DOM. 请说出来,.append()将附加HTML,而不仅仅是文本,因此不当使用会让攻击者将原始HTML注入您的DOM。

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

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