Jquery AppendTo bug(Jquery AppendTo bug)
我正在使用一些jquery并发现jquery
.appendTo()
函数的奇怪行为。在这里查看此代码。
单击第一列的每个项目时,单击的项目将附加到第二列。
如果单击项目
3
和4
,一切都将按预期工作:它们将按照您单击它们的顺序附加。 当您单击项目1
,他也将被正确添加。问题是当你点击第
2
项时。 此项目位于第1
项内。这个项目没有附加到div的末尾,他被附加在他的老父母之上!
看图像:
现在,我期待的与我得到的相比:
如果再次点击第
2
项,他将被添加到div的末尾。这个行为有一些解释吗? 事实上这是一个错误?
我该如何解决这个问题?
Ps:Jquery
1.10.1
I'm working with some jquery and found a weird behaviour of jquery
.appendTo()
function.Look this code here.
When you click on each item of the first column, the clicked item is appended to the second column.
If you click items
3
and4
, everything will work as expected: They will be appended in the order you clicked them. When you click on item1
, he will be appended correctly too.The problem is when you click on item
2
. This item is inside the item1
.This item is not appended to the end of the div, he' is appended on top of his old parent!
Look the images:
Now, what I was expecting versus what I get:
If you click again on item
2
he will be appended at the end of the div.This behaviour have some explanation? It's a bug in fact?
How can I workaround this?
Ps: Jquery
1.10.1
原文:https://stackoverflow.com/questions/29837294
最满意答案
看看这是否是你想要做的:
// Create a function for array mapping // Note that my regex abilities are not great, so I can guarantee // someone else can do it to where this portion is not necessary function filterEmpty($v) { $v = trim($v); return $v; } function getDatesFromFile($filename) { // Set the file to an array $getFile = file(__DIR__.$filename,FILE_SKIP_EMPTY_LINES); // Pre-assign some variables $new = $arr = array(); $year = $mo = $day = false; // Set today $today = date('Y-n-j'); // Set tomorrow $tommorow = date("Y-n-j", strtotime("+1 day")); // Set all the months in the year $calendar = array( "JANUAR", "FEBRUAR", "MARS", "APRIL", "MAI", "JUNI", "JULI", "AUGUST", "SEPTEMBER", "OKTOBER", "NOVEMBER", "DESEMBER" ); // Loop through the file array foreach($getFile as $key => $line) { // Trim the line $line = trim($line); // If empty, skip if(empty($line)) continue; // Match the trimmed line for this line: TIRSDAG 28. JUNI 2016 $regex = '/^([^\s|^\t]{1,})([\s|\t]{1,})([\d]{1,})\.([\s]{1,})([^\t]{1,})([\d]{4})/i'; $section = preg_match($regex,$line,$match); if($section) { $mo = trim($match[5]); $mo = array_search($mo,$calendar)+1; $isDate = $match[6].'-'.$mo.'-'.$match[3]; } if(!isset($isDate)) continue; elseif(($isDate != $today) && ($isDate != $tommorow)) continue; if(!isset($new[$isDate])) $new[$isDate] = array(); // If array is set, go through the sub-set data if(preg_match('/^([0-9\.]{4}).*/',$line)) { // Again, someone could do this better, I'm sure preg_match('/^([\d\.]{5})([^a-zA-Z]{1,})(.+)/i',$line,$details); $details = array_filter(array_map('filterEmpty',$details)); unset($details[0]); $details = array_values($details); if(strpos($details[1],' med ') !== false) $hosted = explode(' med ',$details[1]); elseif(strpos($details[1],'-') !== false) $hosted = explode('-',$details[1]); elseif(strpos($details[1],'–') !== false) $hosted = explode('–',$details[1]); $hosted = array_filter(array_map('filterEmpty',$hosted)); $new[$isDate][substr($details[0],0,2)] = $hosted; } } return $new; } $programs = getDatesFromFile('/test.txt'); foreach($programs as $y => $yearRow) { echo '<ul>'.PHP_EOL; foreach($yearRow as $time => $pData) { echo '<li><div>'.number_format($time,2).'</div><div>'.implode('</div><div>',$pData).'</div></li>'.PHP_EOL; } echo '</ul>'.PHP_EOL; }
$programs
为您提供此数组:Array ( [2016-6-3] => Array ( [06] => Array ( [0] => Din morgen [1] => Eivind og Helge (L) ) [09] => Array ( [0] => Formiddag [1] => Hanne (L) ) [12] => Array ( [0] => Lunsj [1] => Egil (L) ) [15] => Array ( [0] => Ettermiddagsrush [1] => Monica (L) ) [18] => Array ( [0] => Max musikk [1] => Hanne (L) ) [20] => Array ( [0] => Max musikk [1] => Monica (L) ) [22] => Array ( [0] => Mot midnatt [1] => de beste balladene på 102 (L) ) [24] => Array ( [0] => Neon, 102 [1] => musikk hele natta! (til 08.00) ) ) [2016-6-2] => Array ( [06] => Array ( [0] => Din morgen [1] => Eivind, Thor Magnar og Helge (L) ) [09] => Array ( [0] => Formiddag [1] => Hanne (L) ) [12] => Array ( [0] => Lunsj [1] => Monica (L) ) [15] => Array ( [0] => Ettermiddagsrush [1] => Egil (L) ) [18] => Array ( [0] => Max musikk [1] => Hanne (L) ) [20] => Array ( [0] => Max musikk [1] => Monica (L) ) [22] => Array ( [0] => Mot midnatt [1] => de beste balladene på 102 (L) ) [24] => Array ( [0] => Neon, 102 [1] => musikk hele natta! (til 06.00) ) ) )
给你HTML(你必须做一些格式化):
<ul> <li><div>6.00</div><div>Din morgen</div><div>Eivind og Helge (L)</div></li> <li><div>9.00</div><div>Formiddag</div><div>Hanne (L)</div></li> <li><div>12.00</div><div>Lunsj</div><div>Egil (L)</div></li> <li><div>15.00</div><div>Ettermiddagsrush</div><div>Monica (L)</div></li> <li><div>18.00</div><div>Max musikk</div><div>Hanne (L)</div></li> <li><div>20.00</div><div>Max musikk</div><div>Monica (L)</div></li> <li><div>22.00</div><div>Mot midnatt</div><div>de beste balladene på 102 (L)</div></li> <li><div>24.00</div><div>Neon, 102</div><div>musikk hele natta! (til 08.00)</div></li> </ul> <ul> <li><div>6.00</div><div>Din morgen</div><div>Eivind, Thor Magnar og Helge (L)</div></li> <li><div>9.00</div><div>Formiddag</div><div>Hanne (L)</div></li> <li><div>12.00</div><div>Lunsj</div><div>Monica (L)</div></li> <li><div>15.00</div><div>Ettermiddagsrush</div><div>Egil (L)</div></li> <li><div>18.00</div><div>Max musikk</div><div>Hanne (L)</div></li> <li><div>20.00</div><div>Max musikk</div><div>Monica (L)</div></li> <li><div>22.00</div><div>Mot midnatt</div><div>de beste balladene på 102 (L)</div></li> <li><div>24.00</div><div>Neon, 102</div><div>musikk hele natta! (til 06.00)</div></li> </ul>
See if this is what you are looking to do:
// Create a function for array mapping // Note that my regex abilities are not great, so I can guarantee // someone else can do it to where this portion is not necessary function filterEmpty($v) { $v = trim($v); return $v; } function getDatesFromFile($filename) { // Set the file to an array $getFile = file(__DIR__.$filename,FILE_SKIP_EMPTY_LINES); // Pre-assign some variables $new = $arr = array(); $year = $mo = $day = false; // Set today $today = date('Y-n-j'); // Set tomorrow $tommorow = date("Y-n-j", strtotime("+1 day")); // Set all the months in the year $calendar = array( "JANUAR", "FEBRUAR", "MARS", "APRIL", "MAI", "JUNI", "JULI", "AUGUST", "SEPTEMBER", "OKTOBER", "NOVEMBER", "DESEMBER" ); // Loop through the file array foreach($getFile as $key => $line) { // Trim the line $line = trim($line); // If empty, skip if(empty($line)) continue; // Match the trimmed line for this line: TIRSDAG 28. JUNI 2016 $regex = '/^([^\s|^\t]{1,})([\s|\t]{1,})([\d]{1,})\.([\s]{1,})([^\t]{1,})([\d]{4})/i'; $section = preg_match($regex,$line,$match); if($section) { $mo = trim($match[5]); $mo = array_search($mo,$calendar)+1; $isDate = $match[6].'-'.$mo.'-'.$match[3]; } if(!isset($isDate)) continue; elseif(($isDate != $today) && ($isDate != $tommorow)) continue; if(!isset($new[$isDate])) $new[$isDate] = array(); // If array is set, go through the sub-set data if(preg_match('/^([0-9\.]{4}).*/',$line)) { // Again, someone could do this better, I'm sure preg_match('/^([\d\.]{5})([^a-zA-Z]{1,})(.+)/i',$line,$details); $details = array_filter(array_map('filterEmpty',$details)); unset($details[0]); $details = array_values($details); if(strpos($details[1],' med ') !== false) $hosted = explode(' med ',$details[1]); elseif(strpos($details[1],'-') !== false) $hosted = explode('-',$details[1]); elseif(strpos($details[1],'–') !== false) $hosted = explode('–',$details[1]); $hosted = array_filter(array_map('filterEmpty',$hosted)); $new[$isDate][substr($details[0],0,2)] = $hosted; } } return $new; } $programs = getDatesFromFile('/test.txt'); foreach($programs as $y => $yearRow) { echo '<ul>'.PHP_EOL; foreach($yearRow as $time => $pData) { echo '<li><div>'.number_format($time,2).'</div><div>'.implode('</div><div>',$pData).'</div></li>'.PHP_EOL; } echo '</ul>'.PHP_EOL; }
$programs
gives you this array:Array ( [2016-6-3] => Array ( [06] => Array ( [0] => Din morgen [1] => Eivind og Helge (L) ) [09] => Array ( [0] => Formiddag [1] => Hanne (L) ) [12] => Array ( [0] => Lunsj [1] => Egil (L) ) [15] => Array ( [0] => Ettermiddagsrush [1] => Monica (L) ) [18] => Array ( [0] => Max musikk [1] => Hanne (L) ) [20] => Array ( [0] => Max musikk [1] => Monica (L) ) [22] => Array ( [0] => Mot midnatt [1] => de beste balladene på 102 (L) ) [24] => Array ( [0] => Neon, 102 [1] => musikk hele natta! (til 08.00) ) ) [2016-6-2] => Array ( [06] => Array ( [0] => Din morgen [1] => Eivind, Thor Magnar og Helge (L) ) [09] => Array ( [0] => Formiddag [1] => Hanne (L) ) [12] => Array ( [0] => Lunsj [1] => Monica (L) ) [15] => Array ( [0] => Ettermiddagsrush [1] => Egil (L) ) [18] => Array ( [0] => Max musikk [1] => Hanne (L) ) [20] => Array ( [0] => Max musikk [1] => Monica (L) ) [22] => Array ( [0] => Mot midnatt [1] => de beste balladene på 102 (L) ) [24] => Array ( [0] => Neon, 102 [1] => musikk hele natta! (til 06.00) ) ) )
Gives you HTML (you'd have to do some formatting):
<ul> <li><div>6.00</div><div>Din morgen</div><div>Eivind og Helge (L)</div></li> <li><div>9.00</div><div>Formiddag</div><div>Hanne (L)</div></li> <li><div>12.00</div><div>Lunsj</div><div>Egil (L)</div></li> <li><div>15.00</div><div>Ettermiddagsrush</div><div>Monica (L)</div></li> <li><div>18.00</div><div>Max musikk</div><div>Hanne (L)</div></li> <li><div>20.00</div><div>Max musikk</div><div>Monica (L)</div></li> <li><div>22.00</div><div>Mot midnatt</div><div>de beste balladene på 102 (L)</div></li> <li><div>24.00</div><div>Neon, 102</div><div>musikk hele natta! (til 08.00)</div></li> </ul> <ul> <li><div>6.00</div><div>Din morgen</div><div>Eivind, Thor Magnar og Helge (L)</div></li> <li><div>9.00</div><div>Formiddag</div><div>Hanne (L)</div></li> <li><div>12.00</div><div>Lunsj</div><div>Monica (L)</div></li> <li><div>15.00</div><div>Ettermiddagsrush</div><div>Egil (L)</div></li> <li><div>18.00</div><div>Max musikk</div><div>Hanne (L)</div></li> <li><div>20.00</div><div>Max musikk</div><div>Monica (L)</div></li> <li><div>22.00</div><div>Mot midnatt</div><div>de beste balladene på 102 (L)</div></li> <li><div>24.00</div><div>Neon, 102</div><div>musikk hele natta! (til 06.00)</div></li> </ul>
相关问答
更多-
当您需要打破循环并返回某些内容时,您不能使用forEach ,因为它不会中断。 你可以使用老式的 for循环来实现它。 引用Array.prototype.forEach | MDN , 没有办法停止或破坏forEach()循环,而不是抛出异常。 如果你需要这样的行为,forEach()方法是错误的工具,请使用纯循环代替。 像这样的东西: angular.module('app').filter('latinTerm', function() { var terms = [ { ...
-
将foreach嵌入foreach(Nesting foreach in foreach)[2022-04-13]
对于基元(和对象引用): const result = A.filter( el => B.includes(el)); 对于对象键平等: const result = A.filter( el => B.some( el2 => el.value === el2.value ) ); 嵌套的forEachs是完全有效的,但在这种情况下,我更喜欢简单的for循环,因为它们是易碎的: for(const el of A){ for(const el2 of B){ if(e ... -
不是最优的解决方案,但它应该工作: public function getNearest() { $products= Product::orderBy('created_at','DESC')->paginate(5); $user = Auth::user(); $address2 = $user->locatie; //location current user $prepAddr2 = str_replace(' ','+',$ ...
-
Java 8 Stream - 过滤和foreach方法不按预期方式打印(Java 8 Stream - Filter and foreach method not printing as expected)[2023-11-04]
你的期望是错误的。 当执行终端操作forEach ,它一次消耗Stream一个元素。 它消耗的每个元素都必须通过Stream所有中间操作,这会在forEach在同一元素上执行之前(假定元素通过filter )导致filter在元素上执行。 换句话说,当Stream流水线中的下一个操作需要其下一个元素(在你的情况下,下一个操作是forEach )时, filter被延迟地应用到每个元素。 这意味着如果你的终端操作只需要处理一些Stream元素(例如,如果你用findFirst()替换了forEach() fi ... -
我会建议使用一个查询来解决您的问题。 function countViews() { $query = $this->db->query("SELECT COUNT( * ) AS plays , `time`, `track` FROM `views` GROUP BY `track`,DATE( `time` );"); while($row = $query->fetch_assoc()) { $this->db->query(sprintf("INSERT INTO ...
-
foreach ($object as $key => $value) { if ($key === "abc") { unset($object->key); } } 虽然看到你知道密钥(根据你的“if”),你可以这样做: unset($object->abc); foreach ($object as $key => $value) { if ($key === "abc") { unset($object->key); ...
-
由于我没有你的数据结构,我正在提供一个通用的解决方案。 如果我们知道$elements的结构,这可以被优化,但下面的内容应该适合你。 $pageNos = array(); foreach($elements as $el) { $pageno = $el->getAttribute("pageno"); if(!in_array($pageno, $pageNos)) { echo $pageno ; array_push($pageNos,$pageno); ...
-
看看这是否是你想要做的: // Create a function for array mapping // Note that my regex abilities are not great, so I can guarantee // someone else can do it to where this portion is not necessary function filterEmpty($v) { $v = trim($v); retur ...
-
来自文档 : 注意:FOREACH语句只能嵌套到两个级别。 嵌套到三个或更多级别的FOREACH语句将导致语法错误。 您可以在FOREACH中嵌套FOREACH,但不能在其中嵌套另一个嵌套操作。 From the docs: Note: FOREACH statements can be nested to two levels only. FOREACH statements that are nested to three or more levels will result in a grammar ...
-
创建另一个数组来存储我们已经输出的电子邮件地址,然后在每次迭代检查时我们都没有使用该电子邮件地址。 $emails = array(); //array to store unique emails (the ones we've already used) foreach ($scouts as $participant) { $fname = ucfirst($participant['2']); $lname = ucfirst($participant['3']); ...