2017-03-05 86 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 + "']")