开发者

jquery listing HTML elements inside setInterval

开发者 https://www.devze.com 2023-04-04 08:31 出处:网络
I used setinterval to slide number of divs every period of time and that worked fine but the problem happend when i made function\"getdata()\" that testing this animation and returns the width,left po

I used setinterval to slide number of divs every period of time and that worked fine but the problem happend when i made function"getdata()" that testing this animation and returns the width,left postion,text inside Div for each p...please help me to improve the function "getdata()" to get these information about each p which it changes per sec or i value.

ineed to view data like that

1,left is:0,width:60

2,left is:34,width:40

3,left is:66,width:70

I want to make the data_text which is "1 or 2 or 3" is fixed while the width & data_p_l changes for each data_text "u can consider it it's an ID for the element" 4Ex "

1,left is:0,width:60

1>> this is fixed and this line won't be repeated

left is:20>> changing

width:20>> changing

Ihopt that i've cleared my question. Thanks alot.

The HTML:

<div id="test"></div>
<div id="center">
    <p id="th">3</p>
    <p id="s">2</p>
    <p id="f">1</p>
</div>

The jQuery:

$(document).ready(function(){
    var i = null;
    var width = $('#center').width();
    var timer = setInterval(function() {
        $('p').each(function() {
            $(this).css({'left': $(this).position().left + i});
        });
        getdata('p' ,'#test');   
        i+=1;  
    },500);  

function getdata(p开发者_运维百科arentdiv,showdiv){
$(parentdiv).each(function(){
var $this = $(this);    
var width = $this.width();
var data_p_l= $this.position().left;
var data_text= $this.text();
var dataset = data_text + ",Left value is: "+ data_p_l + ",
width value is: "+  width ;//+ ",id value is: "+ data_id;
$(showdiv).text($(showdiv).text() + ' ' + dataset); 
});} 
});


I'm not exactly sure what you are trying to do, but This might help. First in this section:

function getdata(parentdiv,showdiv){
        $(parentdiv).each(function(){
            var len = $( parentdiv ).length;
            var width = $(e).width();
            var data_p_l= $(e).position().left;
            var data_text= $(e).text();
            var dataset = data_text + ",Left value is: "+ data_p_l + ",width value    is: "+ width;
            $(showdiv).text(dataset);
        });
    }

You want to replace e with this.

function getdata(parentdiv,showdiv){
    $(parentdiv).each(function(){
        var len = $( parentdiv ).length;
        var width = $(this).width();
        var data_p_l= $(this).position().left;
        var data_text= $(this).text();
        var dataset = data_text + ",Left value is: "+ data_p_l + ",width value    is: "+ width;
        $(showdiv).text($(showdiv).text() + ' ' + dataset);
            });
        }

Inside the each function, this is used to refer to each element that gets iterated over. At the end of your function, you are putting your results into your div with id test. However, this is running in a loop, so you will only end up with output for the last p tag, and not each one. What I think you want to do is add to the test div like this: $(showdiv).text($(showdiv).text() + ' ' + dataset);

0

精彩评论

暂无评论...
验证码 换一张
取 消