简体   繁体   English

While循环div没有反应

[英]While-loop divs not reacting

I have a list of divs created using a while-loop 我有一个使用while循环创建的div列表

while($row = mysql_fetch_array($result)){
$output = "<div id='drag'>$row[id] - $row[name] - $row['email']</div>";
echo $output;}

When I try to make those divs highlighted for a few seconds. 当我尝试使这些div突出显示几秒钟时。 Only the first div seems to be reacting. 只有第一个div似乎有反应。

$("#drag").effect("highlight", {}, 2000);

Is there any strategy to make all the output be highlighted this way? 是否有任何策略可以以这种方式突出显示所有输出? And is there a way to make a certain div or divs highlighted? 有没有一种方法可以使某个div或多个div突出显示?

IDs must be unique by definition. 根据定义,ID必须唯一。 You can use classes instead. 您可以改用类。

while($row = mysql_fetch_array($result)){
    $output = "<div class='drag'>$row[id] - $row[name] - $row['email']</div>";
    echo $output;
}

Then... 然后...

$(".drag").effect("highlight", {}, 2000);

id is used to give a div unique id. id用于赋予div唯一ID。 so in the while loop you are giving same id in the loop to all div. 因此,在while循环中,您在循环中为所有div提供了相同的ID。 that's why when you are trying to highlight, it is effecting first div. 这就是为什么当您尝试突出显示时,它会影响第一格。

try this code: 试试这个代码:

while($row = mysql_fetch_array($result)){
    $output = "<div class='drag'>$row[id] - $row[name] - $row['email']</div>";
    echo $output;
}

after that apply effect to that class drag 之后将效果应用于该类拖动

$(".drag").effect("highlight", {}, 2000);

hope it will help you :) 希望它能对您有所帮助:)

Using multiple items in a page with the same id is disallowed and, though it may work for some use cases, will cause problems in others (as you're seeing). 不允许在具有相同ID的页面中使用多个项目,尽管它可能在某些用例中可行,但会在其他用例中引起问题(如您所见)。 In general, you'll want to use classes unless ids are required. 通常,除非需要ID,否则您将要使用类。 I'd also recommend that you specify the selector more distinctly that you would with the id (ie, more elements). 我还建议您像使用id(即,更多元素)那样更清楚地指定选择器。

For my own code, I tend to use an id for the page body (ie, which page it is) and distinct elements on the page (header content, main content, right rail content, footer content). 对于我自己的代码,我倾向于将id用于页面主体(即,它是哪个页面)以及页面上的不同元素(页眉内容,主要内容,右栏内容,页脚内容)。 It's rare that I use ids anywhere else (though it's been known to happen). 我很少在其他任何地方使用id(尽管已经知道会发生这种情况)。

Once you switch it over to a class, you can easily change your JS to do something like 一旦将其切换到一个类,就可以轻松更改JS以执行类似的操作

$("div.drag").effect("highlight", {}, 2000);

If possible, adding something more distinct to the div.drag path so that you won't wind up effecting other parts of the page you hadn't considered/written when you worked on this. 如果可能的话,在div.drag路径中添加更独特的内容,以免影响到您在进行此操作时未考虑/编写的页面的其他部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM