开发者

how to use the dynamic id in javascript or jquery?

php code: the following code is

<div id="code'.$row->nid.'" class="cptext" rel="'.$url.'">'.$row->node_value.'</div> 
<div id="cd'.$row->nid.'" style="display: none; border: 1px solid #ccc; ">test test</div>';

the above code will generate id's like this: code1, code2, code3, code4, code5, ... now, in my js file i want to u开发者_Python百科se the id value.

function init() {   
    clip.setHandCursor( true );
        $('this i want to put the id value in which from the above').mouseover( function() {
  }


You can access them in jQuery by using the Attribute Starts With selector. My example will select the ones with an ID that starts with code.

$('[id^="code"]').whatever();

jsFiddle Demo

But I'd say it would be nicer and more logical to give these elements a class they share, so you could easily do selections.

<div id="code'.$row->nid.'" class="cptext code"...

and then just $('.code')....


var theId = $(this).attr('id');

You can just look up the jquery documentation (or google for instance?)


Using jquery, $('#code1').text() will give you the text value.

UPDATE based on updated question:

Since you already have a class set:

$('.cptext').mouseover( function() {...} );


<script type="text/javascript">
    var myid = <?php echo $row->nid; ?>;
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜