2017-03-05 85 views
0

在#main DIV表假设具有ID foo_userId其表示用户内容所具有的ID用户idjQuery的复杂选择器:层次字符串变量

我必须选择与用户id 1234表具有给定id作为可变

<div id="main"> 
    <table id="foo_1234_m"></table> //select this 
    <table id="f0006458_m"></table> 
    <table id="f00_n"></table> 
    <table id="foo_9999_k"></table>   
</div> 


$('div#main>table#foo_1234_m') 
// foo : string , 1234 : variable , m variable 
//////////i'm trying something like : 

var userId='1234'     
var tableID='foo_' + userId   // from this 

$("div#main>table[id^='foo_1234']") // how to make this 

回答

0

看起来你有正确的选择器语法。所以,你可以使用相同字符串连接在你的榜样,写jQuery选择例如为:

var userId = '1234' 
var tableId = 'foo_' + userId 
var element = $("div#main>table[id^='" + tableId + "']")