2

i found a link to get the id's which are ending with a specific string.

  $("element[id$='txtTitle']")

How can we get the id's if ending strings are many. Like i have an array of strings and want all id's that are ending with anyone of these strings.

Thank's

4

4 回答 4

3

You can combine selector with different endings with comma.

$("element[id$='txtTitle1'],  element[id$='txtTitle2'])")

When you have different ending and same start use start.

$("element[id^='txtTitle']")

When you have some text common but not sure if it is in start or end or middle use *

 $("element[id*='txtTitle']")
于 2012-12-13T10:08:01.443 回答
1

如果你有一个包含结尾的字符串数组,id你可以遍历它们并将它们添加到集合中。尝试这个:

var $elements = $(); // empty jQuery object
var ids = ["txtTitle", "txtFirstname", "txtLastName"];
$.each(ids, function(index, value) {
    $elements.add("element[id$='" + value + "']");
});

// $elements is now a collection you can use eg:
$elements.css("background-color", "#C00");
于 2012-12-13T10:14:08.047 回答
0

首先构建选择器:

var endings = ["a","b","c"];
var selector = "";

for(var i = 0, len = endings.length; i < len; i++) {
   selector += "element[id$='" + endings[i] + "']";

   if (i != (endings.length - 1)) selector += ",";
}

然后进行选择:

var el = $(selector);
于 2012-12-13T10:10:54.133 回答
0

所以你有一系列的结局

var arr = ["ending1","ending2"];

您可以使用 jquery.map()函数来构建一个多重选择器(用逗号分隔)

var selector = $.map(arr,function(i,e){return "element[id$='" + e + "']";})
                .join(",");
// result: "element[id$='ending1'],element[id$='ending2']"

然后使用选择器:

var $elements = $(selector);
于 2012-12-13T10:12:44.303 回答