douhuan1901
douhuan1901
2013-12-14 09:12

在PHP代码中单击链接javascript函数调用

<?PHP 
$result = mysql_query($query);
$finalval=0;
while($row = mysql_fetch_array($result))
{
$finalval=$finalval."<a onClick='showContent("please display")' 
href='#' >".  $row['Title']   ."</a> <br>" ;
}
echo  $finalval;
 ?>

    <script language="javascript" type="text/javascript"  >

function showContent(value)
{
alert value;

}
</script>

Guys please help me out and please please tell me in a php code on clicking link how to call javascript/ajax function..Thank you so much

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dongzhuifeng1843 dongzhuifeng1843 8年前

    Try this one ,opening double quotes within double quotes you need to escape them \

    $finalval=$finalval."<a onClick='showContent(\"please display\")' 
    href='#' >".  $row['Title']   ."</a> <br>" ;
    }
    
    点赞 评论 复制链接分享
  • duanji2772 duanji2772 8年前
        <?PHP
            $result = mysql_query($query);
            $finalval = 0;
            while ($row = mysql_fetch_array($result)) {
                 $finalval = $finalval."<a class='show-content' data-value='please display' href='#' >".  $row['Title']   ."</a> <br>" ;
            }
            echo $finalval;
        ?>
    

    With javascript querySelectorAll get all a links with class selector then handle them

         <script language="javascript" type="text/javascript"  >
            var element = document.querySelectorAll(".show-content");
            for (var link in element) {
               element[link].onclick = function() {
                     showContent(this.getAttribute('data-value'));
               };
            }
           function showContent(value){
               alert(value);
           }
         </script>
    

    with jQuery it is pretty stright forward,

       <script language="javascript" type="text/javascript"  >
          $(document).ready(function(){
             $("a.content").on('click', function(){
                  alert($(this).data('value')); 
                  // or make a specific function call
             }); 
          });
      </script>
    

    Hope this will help you, and do not forget to escape the special charactors as mentioned by M Khalid Junaid

    点赞 评论 复制链接分享
  • doumouyi4039 doumouyi4039 8年前

    try alert(value) instead of alert value

    点赞 评论 复制链接分享

相关推荐