JQuery:动态添加 5 个 id(而不是类)
我正在处理这种情况:
- 我有一个以 id 为目标的彩色动画脚本(它不是 JQuery,而是纯 javascript),
- 然后我有一个没有 ids 的动态列表:'ul' somePHP 抛出列表项 '/ul'
- 最后我有JQuery 本身(我将使用它来动态添加几个 id 到列表项:但我仍然不知道如何)
我意识到我无法使用 JQuery 将单个类添加到列表项,因为颜色插件仅按 id 搜索(它使用 getElementById,它与类没有并行性)。
我必须排除 addClass JQ 函数,该函数很容易投入使用。
只有当我可以在列表中插入 id(5 个定义明确的 id),并稍后在外部化颜色插件中给出一些指令来影响它们时,该脚本才会起作用:
<ul>
<li><a href="" id="ontheflyone">pulled from the DB</a></li>
<li><a href="" id="ontheflytwo">pulled from the DB</a></li>
<li><a href="" id="ontheflythree">pulled from the DB</a></li>
<li><a href="" id="ontheflyfour">pulled from the DB</a></li>
<li><a href="" id="ontheflyfive">pulled from the DB</a></li>
</ul>
然后,指令:
document.getElementById(’id’).onmouseover = function() { 'ontheflyone','text','$color1' };
document.getElementById(’id’).onmouseout = function() { 'ontheflyone','text','$color2'};
document.getElementById(’id’).onmouseover = function() { 'ontheflytwo','text','$color1' };
document.getElementById(’id’).onmouseout = function() { 'ontheflytwo','text','$color2'};
etc.
我可以更改插件的 javascript 本身,但我认为使用 JQuery 以某种方式在我的 html 中添加 ids 会更容易。
颜色插件是 Michael Leigeber 编写的一段很棒的代码 我重现如下:
// main function to process the fade request //
function colorFade(id,element,start,end,steps,speed) {
var startrgb,endrgb,er,eg,eb,step,rint,gint,bint,step;
var target = document.getElementById(id);
steps = steps || 20;
speed = speed || 20;
clearInterval(target.timer);
endrgb = colorConv(end);
er = endrgb[0];
eg = endrgb[1];
eb = endrgb[2];
if(!target.r) {
startrgb = colorConv(start);
r = startrgb[0];
g = startrgb[1];
b = startrgb[2];
target.r = r;
target.g = g;
target.b = b;
}
rint = Math.round(Math.abs(target.r-er)/steps);
gint = Math.round(Math.abs(target.g-eg)/steps);
bint = Math.round(Math.abs(target.b-eb)/steps);
if(rint == 0) { rint = 1 }
if(gint == 0) { gint = 1 }
if(bint == 0) { bint = 1 }
target.step = 1;
target.timer = setInterval( function() { animateColor(id,element,steps,er,eg,eb,rint,gint,bint) }, speed);
}
// incrementally close the gap between the two colors //
function animateColor(id,element,steps,er,eg,eb,rint,gint,bint) {
var target = document.getElementById(id);
var color;
if(target.step <= steps) {
var r = target.r;
var g = target.g;
var b = target.b;
if(r >= er) {
r = r - rint;
} else {
r = parseInt(r) + parseInt(rint);
}
if(g >= eg) {
g = g - gint;
} else {
g = parseInt(g) + parseInt(gint);
}
if(b >= eb) {
b = b - bint;
} else {
b = parseInt(b) + parseInt(bint);
}
color = 'rgb(' + r + ',' + g + ',' + b + ')';
if(element == 'background') {
target.style.backgroundColor = color;
} else if(element == 'border') {
target.style.borderColor = color;
} else {
target.style.color = color;
}
target.r = r;
target.g = g;
target.b = b;
target.step = target.step + 1;
} else {
clearInterval(target.timer);
color = 'rgb(' + er + ',' + eg + ',' + eb + ')';
if(element == 'background') {
target.style.backgroundColor = color;
} else if(element == 'border') {
target.style.borderColor = color;
} else {
target.style.color = color;
}
}
}
// convert the color to rgb from hex //
function colorConv(color) {
var rgb = [parseInt(color.substring(0,2),16),
parseInt(color.substring(2,4),16),
parseInt(color.substring(4,6),16)];
return rgb;
}
所以,如果有人能告诉我如何动态添加这些 id,或者如何将 javascript 更改为目标类,非常
感谢,非常感谢,
Jan
I'm dealing with this situation :
- I have a color animation script which targets by id (it's not JQuery but pure javascript)
- then I have a dynamic list without ids : 'ul' somePHP throwing list items '/ul'
- and finally I have JQuery itself (which I'll use to add several ids on the fly to the list items: but I still don't know how)
I've realised that I couldn't add, with JQuery, single classes to the list items, as the color plugin only searches by id (it uses getElementById, which have no parallel with classes).
I must exclude so the addClass JQ function, which would be easy enough to put at work.
The script only would work if I could insert ids in the list, 5 well-defined ids, and give later some instructions in the externalised color plugin to affect them :
<ul>
<li><a href="" id="ontheflyone">pulled from the DB</a></li>
<li><a href="" id="ontheflytwo">pulled from the DB</a></li>
<li><a href="" id="ontheflythree">pulled from the DB</a></li>
<li><a href="" id="ontheflyfour">pulled from the DB</a></li>
<li><a href="" id="ontheflyfive">pulled from the DB</a></li>
</ul>
Then, the instructions :
document.getElementById(’id’).onmouseover = function() { 'ontheflyone','text','$color1' };
document.getElementById(’id’).onmouseout = function() { 'ontheflyone','text','$color2'};
document.getElementById(’id’).onmouseover = function() { 'ontheflytwo','text','$color1' };
document.getElementById(’id’).onmouseout = function() { 'ontheflytwo','text','$color2'};
etc.
I could change the plugin's javascript itself, but I thought it would be easier using JQuery to add, in some way, ids in my html.
The color plugin is an awesome piece of code written by Michael Leigeber
I reproduce as follows :
// main function to process the fade request //
function colorFade(id,element,start,end,steps,speed) {
var startrgb,endrgb,er,eg,eb,step,rint,gint,bint,step;
var target = document.getElementById(id);
steps = steps || 20;
speed = speed || 20;
clearInterval(target.timer);
endrgb = colorConv(end);
er = endrgb[0];
eg = endrgb[1];
eb = endrgb[2];
if(!target.r) {
startrgb = colorConv(start);
r = startrgb[0];
g = startrgb[1];
b = startrgb[2];
target.r = r;
target.g = g;
target.b = b;
}
rint = Math.round(Math.abs(target.r-er)/steps);
gint = Math.round(Math.abs(target.g-eg)/steps);
bint = Math.round(Math.abs(target.b-eb)/steps);
if(rint == 0) { rint = 1 }
if(gint == 0) { gint = 1 }
if(bint == 0) { bint = 1 }
target.step = 1;
target.timer = setInterval( function() { animateColor(id,element,steps,er,eg,eb,rint,gint,bint) }, speed);
}
// incrementally close the gap between the two colors //
function animateColor(id,element,steps,er,eg,eb,rint,gint,bint) {
var target = document.getElementById(id);
var color;
if(target.step <= steps) {
var r = target.r;
var g = target.g;
var b = target.b;
if(r >= er) {
r = r - rint;
} else {
r = parseInt(r) + parseInt(rint);
}
if(g >= eg) {
g = g - gint;
} else {
g = parseInt(g) + parseInt(gint);
}
if(b >= eb) {
b = b - bint;
} else {
b = parseInt(b) + parseInt(bint);
}
color = 'rgb(' + r + ',' + g + ',' + b + ')';
if(element == 'background') {
target.style.backgroundColor = color;
} else if(element == 'border') {
target.style.borderColor = color;
} else {
target.style.color = color;
}
target.r = r;
target.g = g;
target.b = b;
target.step = target.step + 1;
} else {
clearInterval(target.timer);
color = 'rgb(' + er + ',' + eg + ',' + eb + ')';
if(element == 'background') {
target.style.backgroundColor = color;
} else if(element == 'border') {
target.style.borderColor = color;
} else {
target.style.color = color;
}
}
}
// convert the color to rgb from hex //
function colorConv(color) {
var rgb = [parseInt(color.substring(0,2),16),
parseInt(color.substring(2,4),16),
parseInt(color.substring(4,6),16)];
return rgb;
}
So, a million thanks if someone can tell me how to add those ids on the fly, or, maybe, how to change the javascript to target classes,
Many thanks,
Jan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么您有一个普通的
并且想要为其中的每个
提供一个唯一的 id?
尝试 jQuery 命令 "each":
这将循环遍历所有
元素并为每个元素分配其数字索引(在 jQuery 已找到的元素数组中)作为“id”属性。
So you have an ordinary
<ul></ul>
and want to give every<li>
in it a unique id?Try the jQuery command "each":
This will loop over all
<li>
elements and assign every element its nummerical index (in the array of elements jQuery has found) as "id" attribute.如果您使用 jquery,您可以使用如下语法按类获取元素(假设 css 类称为“className”:
要使用 id 执行相同的操作,您可以在 jquery 中执行此操作:
If you're using jquery you can get an element by class using syntax like this (assuming the css class is called 'className':
to do the same thing with an id you would do this in jquery: