如果单击我的8x8表中的任何链接,如何运行函数?

时间:2015-08-30 15:17:28

标签: javascript html

我有一个8x8的hrefs表,用于座位预订网站(这是一项单一的任务)。当点击任何一个时,我想要一个javascript函数来运行。当你有一个链接,即getElementById()和onclick时,我得到了如何做到这一点,但我想要的是一种方法将相同的功能应用于所有的链接,而不做64个唯一的ID和onclick线...当然这是一个事情?如果我所说的话没有任何意义并且希望我能在截止日期之前对其进行排序,那么很乐意澄清任何事情!

2 个答案:

答案 0 :(得分:0)

首先,你必须学习javascript。我用jquery向你解释一小段代码(你必须添加jquery库才能工作)。

HTML

我们可以添加data-*自定义属性来识别X轴和Y轴。

   <div class="square" data-cell-x="0" data-cell-y="0"></div>
   <div class="square" data-cell-x="1" data-cell-y="0"></div>
   <div class="square" data-cell-x="2" data-cell-y="0"></div>
   <div class="square" data-cell-x="3" data-cell-y="0"></div>
   <div class="square" data-cell-x="4" data-cell-y="0"></div>
   <div class="square" data-cell-x="5" data-cell-y="0"></div>

   <div class="square" data-cell-x="0" data-cell-y="0"></div>
   <div class="square" data-cell-x="1" data-cell-y="1"></div>
   <div class="square" data-cell-x="2" data-cell-y="2"></div>
   <div class="square" data-cell-x="3" data-cell-y="3"></div>
   <div class="square" data-cell-x="4" data-cell-y="4"></div>
   <div class="square" data-cell-x="5" data-cell-y="5"></div>

Javascript

现在,在jquery的帮助下,我们可以添加几行来处理onclick:

 $('.square').each(function() {
       // on each square we can:
      $(this).on('click' , function(e) {
          // prevent default action behaviour (i.e. a link)
          e.preventDefault();
          //catch dataset (data-* attributes)
          var ds = $(this).dataset();
          // alert with result
          alert("X: "+ ds.cellX+" ; Y: "+ds.cellY);
      });
 });
祝你好运!

答案 1 :(得分:0)

试试这个:

public function moveUploadedFile($tmp_name, $destination)
    {   
        if(move_uploaded_file($tmp_name, $destination)){
            $this->image_rotation();
            return true;
        }
}
    public function image_rotation(){
        /* Check if the image is rotated,
         * and if it's rotates. Fix it!
         */
        // $filename = __DIR__ . '/'.$this->location .'/'. $this->name . '.' . $this->mime;
        $filename = $this   ->fullPath;

        $exif = exif_read_data($filename);
        if (isset($exif['Orientation']))
        {
          switch ($exif['Orientation'])
          {
            case 3:
             // Need to rotate 180 deg
                  $degrees = 180;
                  break ;

            case 6:
              // Need to rotate 90 deg clockwise
                  $degrees = -90;
                  break ;

            case 8:
              // Need to rotate 90 deg counter clockwise
                  $degrees = 90;
                  break ;
          }

            if (preg_match("/jpg|jpeg/", pathinfo($filename, PATHINFO_EXTENSION)))
            {
                $image_source = imagecreatefromjpeg($filename);
                $rotate = imagerotate($image_source, $degrees, 0);
                imagejpeg($rotate, $filename, 100);

            }
            if (preg_match("/png/", pathinfo($filename, PATHINFO_EXTENSION)))
            {
               $image_source = imagecreatefrompng($filename);
               $rotate = imagerotate($image_source, $degrees, 0);
               imagepng($rotate, $filename, 100);
            }
            if (preg_match("/gif/", pathinfo($filename, PATHINFO_EXTENSION)))
            {
               $image_source = imagecreatefromgif($filename);
               $rotate = imagerotate($image_source, $degrees, 0);
               imagepng($rotate, $filename, 100);
            }

            imagedestroy($image_source); //free up the memory
            imagedestroy($rotate);  //free up the memory

        }
}

假设你的href位于名为&#34; grid&#34;的div中,这将为每个人做var links = document.getElementById("grid").getElementsByTagName("a"); //get every anchor tag in #grid div for(var i = 0; i < links.length; i++) { //loop through every anchor tag tag var link = links[i]; link.onclick = function(e) { //add an event handler to each anchor tag e.preventDefault(); //prevent the default action from happening, e.g. the href alert("Link clicked!"); //do whatever } } 代码内的任何事情添加一个事件处理程序

<强> http://jsfiddle.net/jofish999/v6kgsmqk/