jquery获取选择器信息并附加到div不工作

时间:2015-07-04 05:26:01

标签: javascript jquery

我想获取调用该函数的元素并将Google地图嵌入到目标div中。我尝试使用此代码但无法正常工作

function myfunction(currentElement){

    var $elemId = $(this).attr("id");
    $( ".$elemId" ).append( "<iframe frameborder="0" scrolling="no" marginheight="0" 
    marginwidth="0" src="https://maps.google.ch/maps?f=q&amp;source=s_q&amp;hl=de&amp;geocode=&amp;q=Bern&amp;aq=&amp;
    sll=46.813187,8.22421&amp;sspn=3.379772,8.453979&amp;
    ie=UTF8&amp;hq=&amp;hnear=Bern&amp;t=m&amp;z=12&amp;
    ll=46.947922,7.444608&amp;output=embed&amp;iwloc=near"></iframe>");

}
<div class='Flexible-container'>
  <a href="#" id="showmap0" name="showmap0" onclick="myfunction(this);">show</a>
  <div class='showmap0'></div>
</div>

1 个答案:

答案 0 :(得分:1)

对于Id,我们使用#selectId

    function myfunction(currentElement){
     var $elemId = $(this).attr("id");
 $( '#'+$elemId ).append( "<iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.ch/maps?f=q&amp;source=s_q&amp;hl=de&amp;geocode=&amp;q=Bern&amp;aq=&amp; sll=46.813187,8.22421&amp;sspn=3.379772,8.453979&amp; ie=UTF8&amp;hq=&amp;hnear=Bern&amp;t=m&amp;z=12&amp; ll=46.947922,7.444608&amp;output=embed&amp;iwloc=near"></iframe>" );
     }

我希望这个帮助