简体   繁体   English

如何根据ID使用Jquery在两个Div之间添加Div?

[英]How to Append a Div in-between two Divs based on their ID's using Jquery?

If I have these objects: 如果我有这些对象:

<div id='1'></div>
<div id='2'></div>
<div id='3'></div>
<div id='4'></div>
<div id='5'></div>

I have a textbox: 我有一个文本框:

<input type="text">

Where I type in the value: 3.5 to dynamically make (I have the code for this, the next part I need help with): 我键入的值:3.5动态制作(我有代码,我需要帮助的下一部分):

<div id='3.5'></div>

How can I attach it in this order below, without first checking to see the value of every div on the page? 如何在不首先检查页面上每个div的值的情况下按以下顺序附加它?

<div id='1'></div>
<div id='2'></div>
<div id='3'></div>
<div id='3.5'></div>
<div id='4'></div>
<div id='5'></div>

Using: 使用:

$('#What Goes Here If I Dont know the elements on the page?').after('<div id='3.5'>Hey</div>');

Any help is appreciated, 任何帮助表示赞赏,

Taylor 泰勒

IDs in the DOM should not start with a number (they would not be valid). 在DOM的ID 应该以数字开头(他们不会是有效的)。
They should start with a character A-Za-z and then they can have numbers, - and _ following. 它们应该以字符A-Za-z开头,然后它们可以有数字, -_跟随。

but if you want to go your route try this: 但如果你想走你的路线试试这个:

var div = $("<div id='3.5'></div>");
div_id_after = Math.floor(parseFloat(div.get(0).id));

$('#'+div_id_after).after(div);

Here is a fiddle demo: http://jsfiddle.net/maniator/DPMV5/ 这是一个小提琴演示: http//jsfiddle.net/maniator/DPMV5/

这有什么问题?

  $('#3').after('<div id='3.5'>Hey</div>');

Firstly IDs aren't allowed to start with a number. 首先,ID不允许以数字开头。

Ignoring that, if you are only allowing integers, it's easy, just take 1 from the value and insert it, then renumber the rest. 忽略这一点,如果你只允许整数,那很简单,只需从值中取1并插入,然后重新编号。 Otherwise you'll have to walk the DOM to check the values of the others first. 否则,你必须先走DOM来检查其他人的值。

that comparision to add is arithmetic, so unfortunelly you need to iterate over the divs you could do this. 添加的比较是算术,所以不幸的是你需要迭代你可以做到的div。 maybe you should do padding, like 3 = 300000, so 3.5 is 3500000, to do a best comparision. 也许你应该做填充,如3 = 300000,所以3.5是3500000,做一个最好的比较。

Maybe something like that would work: 也许这样的东西会起作用:

var text = yourTextBox.value;
if(var number = Number(text))
{
    $('#'+Math.floor(number)).after('<div id='+text+'>Hey</div>');
} 
else alert("wrong number");

But you should probably check first (with a regex) that the text within the textbox is a legitimate number… 但你应该首先检查(使用正则表达式)文本框中的文本是合法的数字...

updated. 更新。 thanks Neal. 谢谢Neal。

http://jsfiddle.net/FB8xG/3/ http://jsfiddle.net/FB8xG/3/

var newValue = 3.5;

var oldValue = Math.floor(newValue); //3

var oldValueID = ('#' + oldValue);
var newValueID = ('<div id="' + newValue + '">' + newValue + '</div>');

$(oldValueID).after(newValueID);

Live Demo 现场演示

var input = "3.5";
var parent = Math.floor(input);
var dest = $('#div-'+parent);

dest.after(dest.clone().attr('id','div-'+input).text(input));

Note: This will only work once between integers. 注意:这只能在整数之间使用一次。 A more robust method DOES require looking at the IDs of all divs within the destination. 更强大的方法需要查看目标中所有div的ID。

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

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