2

我目前正在和一个朋友一起写一个网站,我需要创建一个 javascript 循环,用于从数据库中提取图像并将它们填充到网格上的 xy 位置。

我们使用的数据库是用 python 和 django 构建的,但现在我正试图让它与一个循环和一个测试图像一起工作。

这是有问题的循环:

function createImages(){

            var picture = document.createElement('img');{

                for (var pic=0; pic < 100; pic++) { 
                    pic.pk = 1;
                    pic.model = 'image';
                    pic.fields.title = 'Image Test';
                    pic.fields.timestamp = '2013-01-01T00:00:00.000Z';
                    pic.fields.image = 'http://i240.photobucket.com/albums/ff301/quyenhiepkhach/CAT.jpg';
                    pic.fields.height = 30 + 'px';
                    pic.fields.width = 30 + 'px';
                    pic.fields.link = '#ImageLink';
                    pic.fields.board = 1;
                    pic.fields.posx = 100 + 'px';
                    pic.fields.posy = 50 + 'px';
                    pic.fields.owner = 1;
                    pic.fields.region = 1;

                    picture.className = 'image-tooltip';
                    picture.src = pic.fields.image;
                    picture.style.marginTop = pic.fields.posy;
                    picture.style.marginLeft = pic.fields.posx;
                    picture.style.height = pic.fields.height;
                    picture.style.width = pic.fields.width;


                    document.body.appendChild(picture);


                }

            }

};

createimages();

到目前为止我所做的工作:

  • 在我的索引页面上绘制的网格有两个部分(主要和标准)。
  • 显示 xy 坐标和标准或主网格空间的鼠标悬停脚本。(不在 jsfiddle 中工作)

到目前为止我已经打破了什么:

  • 用于从数据库中提取图像并将其写入页面正文的 Javascript 循环
  • 鼠标悬停脚本显示一些图像数据

我已经包含了下面的所有内容来制作网页以及一个jsFiddle

HTML 头:

    <!-- Le random script for mouseover -->
    <script type="text/javascript" src="http://code.jquery.com/jquery-git.js"></script>

<!--MOUSEOVER SCRIPT FOR GRID COORDINATES-->
<script>
        $(window).load(function(){
            var tooltip = $( '<div id="tooltip">' ).appendTo( 'body' )[0];

                $( '.coords' ).    
                each(function () {       

                    var pos = $( this ).offset(),
                        top = pos.top,           
                        left = pos.left,          
                        width = $( this ).width(),  
                        height = $( this ).height();       

                    $( this ).
                        mousemove(function ( e ) {
                        var x =  ( (e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft) - left ) .toFixed( 0 ),
                            y =  ( ( (e.clientY + document.body.scrollTop + document.documentElement.scrollTop) - top ) ) .toFixed( 0 );

                        if ( x > 20 && x < 481 && y > 20 && y < 321) {
                            $( tooltip ).text( 'prime | ' + x + ', ' + y ).css({                    
                                left: e.clientX + 20,                    
                                top: e.clientY + 10                
                            }).show();
                        }

                        else {
                            $( tooltip ).text( 'standard | ' + x + ', ' + y ).css({                    
                                left: e.clientX + 20,                    
                                top: e.clientY + 10                
                            }).show();

                        }

                    }).         

                    mouseleave(function () {                
                        $( tooltip ).hide();            
                    });    

                });    

        });

</script>

<!--MOUSEOVER SCRIPT FOR IMAGES-->
<script> 
                $(window).load(function(){
            var imagetooltip = $( '<div id="imagetooltip">' ).appendTo( 'body' )[0];

            $( '.image-tooltip' ).    
                each(function () {      

                    $( imagetooltip ).text( pic.fields.title + ' , ' + pic.fields.link ).css({                    
                        left: e.clientX + 20,                    
                        top: e.clientY + 10                
                    }).show();




                    mouseleave(function () {                
                        $( tooltip ).hide();            
                    });     
                });    
        });

</script>

CSS:

        /* Style for standard section on grid */

        .grid {
            margin: 0px auto auto;
            border: 1px solid #000;
            border-width: 0 1px 1px 0;
            background-color: #28ACF9;
        }

        /* Style for grid div */

        .grid div {
            border: 1px solid #000;
            border-width: 1px 0 0 1px;
            float: left;
        }

        /* Style for prime section on grid */

        .gridprime {
            margin-top: 50px ;
            margin-left: 50px;
            border: 1px solid #000;
            background-color: #FFFF33;
            float: left;
        }

        /* Style for grid coords tooltip */

        #tooltip { 
            text-align:center; 
            background:black; 
            color:white; 
            padding:3px 0; 
            width:150px; 
            position:fixed; 
            display:none; 
            white-space:nowrap;
            z-index:3; 
        }


        /* Style for image tooltip */

        #imagetooltip { 
            text-align:left; 
            background:#CCC; 
            color:white; 
            padding:3px 0; 
            width:200px; 
            position:fixed; 
            display:none; 
            white-space:nowrap;
            z-index:4; 
        }

HTML 正文:

<!--SCRIPT TO CREATE THE GRID (WORKING)-->
<script type="text/javascript">

function creategrid(size){

            var primeW = Math.floor((460) / size),
                primeH = Math.floor((300) / size),
                standardW = Math.floor((500) / size),
                standardH = Math.floor((500) / size);

            var standard = document.createElement('div');
                standard.className = 'grid coords';
                standard.style.width = (standardW * size) + 'px';
                standard.style.height = (standardH * size) + 'px';
                standard.board = '1';

            var prime = document.createElement('div');
                prime.className = 'gridprime';
                prime.style.width = (primeW * size) + 'px';
                prime.style.height = (primeH * size)+ 'px';
                prime.style.position = 'absolute'
                prime.style.zIndex= '1';
                standard.appendChild(prime);

            for (var i = 0; i < standardH; i++) {

                for (var p = 0; p < standardW; p++) {

                    var cell = document.createElement('div');
                        cell.style.height = (size - 1) + 'px';
                        cell.style.width = (size - 1) + 'px';
                        cell.style.position = 'relative'
                        cell.style.zIndex= '2';
                        standard.appendChild(cell);

            }

        }

        document.body.appendChild(standard);

    }

    creategrid(10);

</script>



<!--SCRIPT TO LOOP IMAGES OUT OF DATABASE (USING 1 TO TEST FOR NOW)-->

<script type="text/javascript">

function createImages(){

            var picture = document.createElement('img');{

                for (var pic=0; pic < 100; pic++) { 
                    pic.pk = 1;
                    pic.model = 'image';
                    pic.fields.title = 'Image Test';
                    pic.fields.timestamp = '2013-01-01T00:00:00.000Z';
                    pic.fields.image = 'http://i240.photobucket.com/albums/ff301/quyenhiepkhach/CAT.jpg';
                    pic.fields.height = 30 + 'px';
                    pic.fields.width = 30 + 'px';
                    pic.fields.link = '#ImageLink';
                    pic.fields.board = 1;
                    pic.fields.posx = 100 + 'px';
                    pic.fields.posy = 50 + 'px';
                    pic.fields.owner = 1;
                    pic.fields.region = 1;

                    picture.className = 'image-tooltip';
                    picture.src = pic.fields.image;
                    picture.style.marginTop = pic.fields.posy;
                    picture.style.marginLeft = pic.fields.posx;
                    picture.style.height = pic.fields.height;
                    picture.style.width = pic.fields.width;

                    if (pic.fields.board = document.body.id);{
                        document.body.appendChild(picture);
                    }

                }

            }

        };

        createimages();

</script>
4

2 回答 2

1

您的代码中有各种错误

pic是一个数字,但您似乎正在为其设置属性,因为它是一个对象文字

for (var pic=0; pic < 100; pic++) { 
   pic.pk = 1;

此行也将失败,因为您需要首先创建pic.fields对象

pic.fields = {}; // <-- add this line
pic.fields.title = 'Image Test';

您的函数已被调用createImages,但您正在尝试调用createimages(区分大小写)

我建议你查看你的浏览器控制台(通常是 F12)来检查错误

于 2013-01-26T16:09:37.010 回答
1

您的代码充满了语法错误和逻辑问题。首先使用浏览器控制台查看错误并进行相应修复。

还要注意 javascript 是区分大小写的,所以如果你创建一个函数createImages(),你需要使用相同的大小写来调用函数。你打电话给createimages()不存在的

您不能使用作为变量在计数器所在的循环中pic创建对象。forpic

还需要在循环内而不是在循环外创建新图像。

工作代码:

//SCRIPT TO LOOP IMAGES OUT OF DATABASE (USING 1 TO TEST FOR NOW)//
function createImages() {

    for (var pic = 0; pic < 100; pic++) {
         /* new image for each pass of loop*/
        var picture = document.createElement('img');
        var data = {
            pk: 1,
            model: 'image',
            fields: {
                title: 'Image Test',
                timestamp: '2013-01-01T00:00:00.000Z',
                image: 'http://i240.photobucket.com/albums/ff301/quyenhiepkhach/CAT.jpg',
                height: 30 + 'px',
                width: 30 + 'px',
                link: '#ImageLink',
                board: 1,
                posx: 100 + 'px',
                posy: 50 + 'px',
                owner: 1,
                region: 1
            }
        };

        picture.className = 'image-tooltip';
        picture.src = data.fields.image;
        picture.style.marginTop = data.fields.posy;
        picture.style.marginLeft = data.fields.posx;
        picture.style.height = data.fields.height;
        picture.style.width = data.fields.width;
       /* comment out "if" since isn't true*/
       // if (data.fields.board ==document.body.id) {
            document.body.appendChild(picture);
       // }

    }

}


createImages();

演示:http: //jsfiddle.net/8eYhK/9/

于 2013-01-26T16:23:05.977 回答