使用jquery数据响应更改跨度宽度样式

问题描述:

I want to change the width of span depends on load data from jquery respnse. Thanks.

<div class="star-rating">
     <span style="width:
         <script type='text/javascript'> 
             var auto_refresh = setInterval(function(){ 
             $('#load_rating_average').load('get_average_rating.php?b_id=<?php echo $row['b_id']; ?>'); 
             },2000);
         </script>
         <span id=''></span>
     </span>
</div>

我想更改span的宽度取决于来自jquery respnse的加载数据。 谢谢。 p>

 &lt; div class =“star-rating”&gt; 
&lt; span style =“width:
&lt; script type ='text / javascript'  &gt; 
 var auto_refresh = setInterval(function(){
 $('#load_rating_average')。load('get_average_rating.php?b_id =&lt;?php echo $ row ['b_id'];?&gt;')  ; 
},2000); 
&lt; / script&gt; 
&lt; span id =''&gt;&lt; / span&gt; 
&lt; / span&gt; 
&lt; / div&gt; 
  code>   pre> 
  div>

This one Worked thanks for sharing.

var auto_refresh = setInterval(
function()
{
  var dt;
  $.get("get_average_rating.php?b_id=<?php echo $row['b_id']; ?>", function(data){
    dt=data;
    $("#load_rating_avg").css("width", dt +"%");
  });
}, 2000);

You can use jQuery css for this, just get the value in javascript and assign it using jQuery

$("#spanId").css("width", "<?php echo $dynamicWidth ?>%"); // give id to span

or

$("#spanId").width("<?php echo $dynamicWidth ?>%");// give id to span

EDIT

you can use ajax request as well,

setInterval(function(){     
    $.ajax({
        url: 'get_average_rating.php',
        type: 'GET',
        data: { 'b_id' : <?php echo $b_id ?> },
        success: function(width){ // assumed width = 75 (numeric o/p)
            // apply width to spanId
         $("#spanId").css("width",width + "%");
        }
    })
},2000);