将png图标添加到HTML

时间:2012-04-28 13:01:40

标签: jquery icons fancybox

我想知道是否有人可以帮助我。

首先道歉,因为对于某些人来说这可能是一个非常基本的问题,但我是新手,所以请耐心等待。

我正在使用下面的脚本在fancyBox中创建一个图片库。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
  <title>Gallery</title>  
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
    <script type="text/javascript" src="fancybox/lib/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="fancybox/source/jquery.fancybox.js?v=2.0.6"></script>
    <script type="text/javascript" src="fancybox/source/helpers/jquery.fancybox-buttons.js?v=1.0.2"></script>
    <script type="text/javascript" src="fancybox/source/helpers/jquery.fancybox-thumbs.js?v=1.0.2"></script>
    <script type="text/javascript" src="fancybox/source/helpers/jquery.fancybox-media.js?v=1.0.0"></script>
    <link rel="stylesheet" type="text/css" href="fancybox/source/helpers/jquery.fancybox-thumbs.css?v=1.0.2" />
    <link rel="stylesheet" type="text/css" href="fancybox/source/helpers/jquery.fancybox-buttons.css?v=1.0.2" />
    <link rel="stylesheet" type="text/css" href="fancybox/source/jquery.fancybox.css?v=2.0.6" media="screen" />


  <script type="text/javascript">  

            $('.fancybox').fancybox({
                openEffect  :   'elastic',
                closeEffect :   'elastic',

                padding :   20,
                fitToView   :   true,

                prevEffect :    'none',
                nextEffect :    'none',

                closeBtn  : false,
                arrows : false,

                helpers : {
                    title : {
                        type : 'inside'
                    },
                    buttons : {}
                },

                afterLoad : function() {
                    this.title = 'Image ' + (this.index + 1) + ' of ' + this.group.length + (this.title ? ' - ' + this.title : '');
                }
            });


</script> 
  <style type="text/css">
<!--
.style1 {
    font-size: 14px;
    margin-top: 5px;
    margin-right: 110px;
}
-->
  </style>  
</head>
<body style="font-family: Calibri; color:  #505050; margin-left: 240px; float:left;"/>
<div align="right" class="style1"> <a href = "javascript:document.gallery.submit()"/> Add Images <a/> &larr; View Uploaded Images </div> 
  <form id="gallery" name="gallery" class="page" action="index.php" method="post" style="margin-left: -120px; margin-right: 50px; border-left: 100; caption-side: left;"> 

    <p>
      <?php for ($i = 0; $i < $descriptions->documentElement->childNodes->length; $i++) :  
                          $xmlFile = $descriptions->documentElement->childNodes->item($i);  
                          $name = htmlentities($xmlFile->getAttribute('originalname'), ENT_COMPAT, 'UTF-8');  
                          $description = htmlentities($xmlFile->getAttribute('description'), ENT_COMPAT, 'UTF-8');  
                          $source = $galleryPath . rawurlencode($xmlFile->getAttribute('source'));  
                          $thumbnail = $thumbnailsPath . rawurlencode($xmlFile->getAttribute('thumbnail'));  
                  ?>
      <a class="fancybox" rel="allimages" title="<?php echo $name; ?>" href="<?php echo $source; ?>"><img src="<?php echo $thumbnail; ?>"alt="<?php echo $description; ?>" /></a>
      <?php endfor; ?>  
    </p>
</form>  
</body>
</html> 

我现在想要添加允许用户删除图像的功能。

使用'famfamfam.com'中的'丝绸图标'我找到了一个合适的图标,我想放在每张图片的左上方,但我不知道如何将图标添加到我的页面。

我试图找到一个在线教学,告诉我如何做到这一点,但到目前为止还没有成功。

我只是想知道是否有人可以提供som指导我如何将其添加到我的页面。

非常感谢和问候

1 个答案:

答案 0 :(得分:2)

就在这一行之前:

<a class="fancybox" rel="allimages" title="<?php echo $name; ?>" href="<?php echo $source; ?>"><img src="<?php echo $thumbnail; ?>"alt="<?php echo $description; ?>" /></a>

您可以添加以下内容:

<img src="path/to/image.png" class="removeImage" alt="Remove this image" />

然后用jQuery做这样的事情:

$('.removeImage').click(function(){
    $(this).next('a.fancybox').remove();
});

我还会对文档做出很多其他更改。例如,删除所有内联样式并将它们组合到外部样式表中是个好主意。你的javascript也一样。此外:

  • 您尝试自行关闭开头标记,如下所示:<body /> - 我们不这样做。
  • 使用font-family时,制作一个网络安全的字体列表总是一个好主意。请查看以下内容:http://cssfontstack.com/
  • 不推荐使用HTML中的align属性

我对您的文档进行了一些更改,因此更容易使用。这是HTML / PHP:

<!doctype html>
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]>    <html class="ie7"> <![endif]-->
<!--[if IE 8 ]>    <html class="ie8"> <![endif]-->
<!--[if IE 9 ]>    <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->
<head>  

<title>Gallery</title>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

<!-- styles -->
<link rel="stylesheet" type="text/css" href="fancybox/source/helpers/jquery.fancybox-thumbs.css?v=1.0.2" />
<link rel="stylesheet" type="text/css" href="fancybox/source/helpers/jquery.fancybox-buttons.css?v=1.0.2" />
<link rel="stylesheet" type="text/css" href="fancybox/source/jquery.fancybox.css?v=2.0.6" media="screen" />
<link rel="stylesheet" type="text/css" href="screen.css" media="screen" />  
</head>

<body>
<div align="right" class="style1"> <a href = "javascript:document.gallery.submit()"/> Add Images <a/> &larr; View Uploaded Images </div> 
<form id="gallery" name="gallery" class="page" action="index.php" method="post">
<p>
<?php for ($i = 0; $i < $descriptions->documentElement->childNodes->length; $i++) :  
                      $xmlFile = $descriptions->documentElement->childNodes->item($i);  
                      $name = htmlentities($xmlFile->getAttribute('originalname'), ENT_COMPAT, 'UTF-8');  
                      $description = htmlentities($xmlFile->getAttribute('description'), ENT_COMPAT, 'UTF-8');  
                      $source = $galleryPath . rawurlencode($xmlFile->getAttribute('source'));  
                      $thumbnail = $thumbnailsPath . rawurlencode($xmlFile->getAttribute('thumbnail'));  
              ?>
  <a class="fancybox" rel="allimages" title="<?php echo $name; ?>" href="<?php echo $source; ?>"><img src="<?php echo $thumbnail; ?>"alt="<?php echo $description; ?>" /></a>
  <?php endfor; ?>  
</p>
</form>

<!-- scripts -->
<script type="text/javascript" src="fancybox/lib/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="fancybox/source/jquery.fancybox.js?v=2.0.6"></script>
<script type="text/javascript" src="fancybox/source/helpers/jquery.fancybox-buttons.js?v=1.0.2"></script>
<script type="text/javascript" src="fancybox/source/helpers/jquery.fancybox-thumbs.js?v=1.0.2"></script>
<script type="text/javascript" src="fancybox/source/helpers/jquery.fancybox-media.js?v=1.0.0"></script>
<script type="text/javascript" src="main.js"></script>
</body>
</html>

...这是你的样式表 - screen.css:

body {font-family: Calibri; color: #505050; margin-left: 240px; float:left;}
.style1 {font-size: 14px; margin-top: 5px; margin-right: 110px;}
#gallery {margin-left: -120px; margin-right: 50px; border-left: 100; caption-side: left;}

最后,在页面准备就绪后运行的脚本 - main.js:

$(function(){
    $('.removeImage').click(function(){
        $(this).next('a.fancybox').remove();
    });

$('.fancybox').fancybox({
            openEffect  :   'elastic',
            closeEffect :   'elastic',

            padding :   20,
            fitToView   :   true,

            prevEffect :    'none',
            nextEffect :    'none',

            closeBtn  : false,
            arrows : false,

            helpers : {
                title : {
                    type : 'inside'
                },
                buttons : {}
            },

            afterLoad : function() {
                this.title = 'Image ' + (this.index + 1) + ' of ' + this.group.length + (this.title ? ' - ' + this.title : '');
            }
        });
})