duanchun6148 2012-08-19 23:31
浏览 35
已采纳

使用jQuery .html()处理PHP

I am trying to get jQuery to change the html of an element and include PHP. It all works fine apart from PHP. Here is what I have:

$('.gridheader').each(function(index){
    $(this).toggle(function (){
        $('.gridimage:eq('+index+'), .gridinfo:eq('+index+')').slideToggle();
        $('.gridheadinfo:eq('+index+')').html('Click to close <img src="<?php echo base_url() ?>/assets/images/closetab.png" width="10" height="10" />');
    }, function(){
        $('.gridimage:eq('+index+'), .gridinfo:eq('+index+')').slideToggle();
        $('.gridheadinfo:eq('+index+')').html('Click for info <img src="<?php echo base_url() ?>/assets/images/scrollup.png" width="11" height="10" />');
    });
});

I have the image originally in there (HTML):

<div class="float-left">Kredible</div><div class="float-right gridheadinfo">Click for info <img src="<?php echo base_url() ?>assets/images/scrollup.png" width="11" height="10" /></div></div>

The HTML works fine, however when I change it using jQuery the image doesn't show. Ive looked at it in the inspector after the change and the PHP shows up as PHP instead of converting to HTML?

Im rather puzzled.

Thanks.

  • 写回答

2条回答 默认 最新

  • dshmvqnl98119 2012-08-19 23:40
    关注

    Save your server-side variable and then use it:

          var url="<?php echo base_url() ?>"; 
        //this will be parsed by the php interpreter at page load so url will be "localhost/subdomain/" probably. 
    
            $('.gridheader').each(function(index){
                $(this).toggle(function (){
                    $('.gridimage:eq('+index+'), .gridinfo:eq('+index+')').slideToggle();
                    $('.gridheadinfo:eq('+index+')').html('Click to close <img src="'+url+'/assets/images/closetab.png" width="10" height="10" />');
        //use it here
                }, function(){
                    $('.gridimage:eq('+index+'), .gridinfo:eq('+index+')').slideToggle();
                    $('.gridheadinfo:eq('+index+')').html('Click for info <img src="'+url+'/assets/images/scrollup.png" width="11" height="10" />');
                });
            });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名