Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php jQuery将相同的功能应用于不同的div_Php_Javascript_Jquery - Fatal编程技术网

Php jQuery将相同的功能应用于不同的div

Php jQuery将相同的功能应用于不同的div,php,javascript,jquery,Php,Javascript,Jquery,我想有10个div,当有人点击其中任何一个或在其中任何一个鼠标悬停时,它都会改变。我现在有一个来自php的foreach(遍历我想展示的内容数组),并以类似的方式向每个foreach写入一个脚本,唯一的区别是div的id: <?php foreach($lines as $line) { $lineId = $line->getId(); echo "$('#translation$lineId').hover(

我想有10个div,当有人点击其中任何一个或在其中任何一个鼠标悬停时,它都会改变。我现在有一个来自php的foreach(遍历我想展示的内容数组),并以类似的方式向每个foreach写入一个脚本,唯一的区别是div的id:

<?php 
        foreach($lines as $line) {
            $lineId = $line->getId();
            echo "$('#translation$lineId').hover(
                function() { $('#translation$lineId').css('background-color', 'yellow'); },
                function() { $('#translation$lineId').css('background-color', 'transparent'); });";
            echo "$('#translation$lineId').focusin(function()
                            { $('#translation$lineId').css('background-color', 'red'); });";
            echo "$('#translation$lineId').focusout(function()
                            { $('#translation$lineId').css('background-color', 'transparent'); });";
        }
    ?>

为什么不改用类呢

标记:

<div id="div1" class="mydivclass">Something</div>
<div id="div2" class="mydivclass">Something Else</div>

当你点击div时,它会变成红色,当你点击其他地方时,它会变成透明的

$(document).ready(function() {
    $('html').click(function(event) {
        $('.mydivclass').each(function() {
            if ($(event.target).closest(this).length) {
                $(this).css('background-color','red');
            }
            else {
                $(this).css('background-color','rgba(0,0,0,0)');
            }
        });
    });
});
对于hover,只需使用
:hover
css伪类

.mydivclass {
    background-color:rgba(0,0,0,0);
}
.mydivclass:hover {
    background-color:red;
}

为什么不改用类呢

标记:

<div id="div1" class="mydivclass">Something</div>
<div id="div2" class="mydivclass">Something Else</div>

当你点击div时,它会变成红色,当你点击其他地方时,它会变成透明的

$(document).ready(function() {
    $('html').click(function(event) {
        $('.mydivclass').each(function() {
            if ($(event.target).closest(this).length) {
                $(this).css('background-color','red');
            }
            else {
                $(this).css('background-color','rgba(0,0,0,0)');
            }
        });
    });
});
对于hover,只需使用
:hover
css伪类

.mydivclass {
    background-color:rgba(0,0,0,0);
}
.mydivclass:hover {
    background-color:red;
}

有许多事情可以做得更好:

1) 不要将JavaScript混合到PHP代码中,即使在您的示例中,您也可以创建一个将id作为参数的函数
2) 缓存jQuery选择器,例如:

var $translation3 = $('#translation3');

$translation3.hover(
  function() { $translation3.css('background-color', 'yellow'); },
  function() { $translation3.css('background-color', 'transparent'); 
});
3) 要优化内容,最重要的是为这些行分配一个类,例如“translation”:

var $translation = $('.translation');

$translation.hover(function() {
  function() { $(this).css('background-color', 'yellow'); },
  function() { $(this).css('background-color', 'transparent');  
});

有许多事情可以做得更好:

1) 不要将JavaScript混合到PHP代码中,即使在您的示例中,您也可以创建一个将id作为参数的函数
2) 缓存jQuery选择器,例如:

var $translation3 = $('#translation3');

$translation3.hover(
  function() { $translation3.css('background-color', 'yellow'); },
  function() { $translation3.css('background-color', 'transparent'); 
});
3) 要优化内容,最重要的是为这些行分配一个类,例如“translation”:

var $translation = $('.translation');

$translation.hover(function() {
  function() { $(this).css('background-color', 'yellow'); },
  function() { $(this).css('background-color', 'transparent');  
});

可以使用jquery类选择器而不是Id。如果为所有transactionLines指定相同的类名,则可以访问所有transactionLines div的悬停事件

所以,你不需要通过这种方式进行交流

交易行1 交易线路2


可以使用jquery类选择器而不是Id。如果为所有transactionLines指定相同的类名,则可以访问所有transactionLines div的悬停事件

所以,你不需要通过这种方式进行交流

交易行1 交易线路2


使用类而不是IDs将对您有很大帮助:使用类而不是IDs将对您有很大帮助:检查此项:检查此项:我将如何为几个div执行此操作?我想和第一个问题一样。也许是,每个人?不能让它工作。谢谢。不知什么原因它不起作用,你能调查一下吗?告诉我c.replace不是一个function@Vadiklk你的意思是你想同时选择多个div吗?让我们看看我如何为多个div选择它?我想和第一个问题一样。也许是,每个人?不能让它工作。谢谢。不知什么原因它不起作用,你能调查一下吗?告诉我c.replace不是一个function@Vadiklk您的意思是希望能够同时选择多个div吗?让我们