0

i have some data in my localStorage like this:

test="[[bla/bla,Bla BLa],[bla1/bla1,Bla1 BLa1]]"

And i have an div with list items (10 list-items) like this

<li data-1="Bla BLa" data-2="bla/bla"></li>
<li data-1="Bla1 BLa1" data-2="bla1/bla1"></li>
<li data-1="Bla2 BLa2" data-2="bla2/bla2"></li>

Now i need to add a border to the list items that have the same data attribute as the value that is stored in the localStorage, in this case the first two.

How can i do this? I am stuck on this one.

Thank you very much

4

1 回答 1

1

假设您在 localStorage 中始终具有相同格式的测试字符串,您可以将其直接解析为 css 选择器http://api.jquery.com/multiple-attribute-selector/

var test="[[bla/bla,Bla BLa],[bla1/bla1,Bla1 BLa1]]", //That comes from storage

    selector = test.substr(2, test.length - 4) //trim [[ ]]
                   .split("],[") //convert to array
                   .map(function(item) { //Convert each pair to a selector
                        item = item.split(",");
                        return "[data-1='" + item[1] + "'][data-2='" + item[0] + "']";
                   }).join(","); //Concat

 var items = $(selector, "#container"); //have fun

http://jsfiddle.net/LNkJB/

于 2013-11-01T13:38:42.233 回答