我正在制作一本电子书(杂志)。但我有 200 页(jpg 格式)。因此,为了避免为 200 个页面编写相同的行代码,我想了解如何使用 javascript 和 php 来完成此操作。

电子书中每个页面的 HTML 代码是:

<div id="page" style="background-image:url(img/2014/image-001.jpg);"></div> 

我进行更改/添加的 JavaScript 函数是:

function myBook(i)  
        { 
            var x = document.getElementById("page"); 
 
            if (x.hasAttribute("style"))  
            { 
                i++; 
                x.setAttribute("style", "background-image:url(img/2014/image-00" + i + ".jpg"); 
            } 
        } 

我调用 myBook() 函数的 php 代码是这样的:

<?php  
    for ($x = 0; $x < 200; $x++) { 
        echo '<script type="text/javascript">myBook($i)</script>';  
    } 
?> 

我想要的只是向 HTML 元素添加(+1)一个数字,以生成正确的 jpg 文件。

提前致谢。

请您参考如下方法:

而不是

$a = (String) $x; 
if (strlen($a)<3) 
{ 
for($y = 3 - strlen($a); $y > 0; $y--) 
{ 
$a = '0' . $a; 
} 
} 

你可以写

$a = str_pad($x,3,"0",STR_PAD_LEFT); 


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!