简体   繁体   中英

Passing javascript variable to jquery

Im facing some problems what i cannot solve for days now.

I wish to get the Position of an element which is have a uniqe ID

Code:

function e_div_show(sid,ctd)
{
 var b_fos="t"+sid;
 var pos = $(b_fos).offset();
 alert(pos.top + ' ' + pos.left);
}

HTML Code:
<?php
...
 echo ('<td ID="t'.$array['S_ID'].'">...</td>
...
 ?>

this is not working..

if i replace the ID to "b_fos" and i comment out the //var b_fos="t"+sid; it working fine.. however in this case the TD ID wont be unique becouse of the php array which generating the code.

Any help please how to define the pos correclty with a javascript variable?

Try changing:

var b_fos = "t" + sid;

To

var b_fos = "#t" + sid;

Try this code you have miss '#' before

function e_div_show(sid,ctd)
{
 var b_fos="#t"+sid;
 var pos = $(b_fos).offset();
 alert(pos.top + ' ' + pos.left);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM