Javascript 替换jQuery中的元素
我试图以编程方式修改jQuery中的一个元素,即docID中的一个数字递增到最大值。我正在替换从下载到查看页面上一系列图像上的文本。如果我使用Javascript 替换jQuery中的元素,javascript,jquery,Javascript,Jquery,我试图以编程方式修改jQuery中的一个元素,即docID中的一个数字递增到最大值。我正在替换从下载到查看页面上一系列图像上的文本。如果我使用#ctl00\cphMainContent\u dlProductList\u ct100\u ctl00\u lnkproof download而不是$(docID)中的docID。文本(…)部分代码,则会正确替换文本。当我在其位置使用docID变量时,它不起作用 我做错了什么 谢谢 var max = 10; var count = 100;
#ctl00\cphMainContent\u dlProductList\u ct100\u ctl00\u lnkproof download
而不是$(docID)中的docID
。文本(…)
部分代码,则会正确替换文本。当我在其位置使用docID
变量时,它不起作用
我做错了什么
谢谢
var max = 10;
var count = 100;
var s1 = "#ctl00_cphMainContent_dlProductList_ct";
var s2 = "_ctl00_lnkProofDownload";
var docID = "";
for (i = 1; i <= max; i++)
{
docID = s1.concat (count++, s2);
$(document).ready(function() {
$(docID).text(function(i, oldText) {
return oldText === 'Download' ? 'View' : oldText;
});
});
}
var max=10;
var计数=100;
var s1=“#ctl00_cphMainContent_dlProductList_ct”;
var s2=“\u ctl00\u lnkproof下载”;
var docID=“”;
对于(i=1;i来说,您的代码中似乎做了一些不正确的事情,包括使用1而不是l。如果它应该是100而不是l00,那么类似这样的东西会起作用:
jQuery(function () {
var max = 10,
count = 100,
s1 = 'ctl00_cphMainContent_dlProductList_ct',
s2 = '_ctl00_lnkProofDownload',
docID;
for (var i = count; i <= count + max; i++) {
docID = s1 + i + s2;
jQuery('#' + docID).text(function (idx, oldText) {
return oldText === 'Download' ? 'View' : oldText;
});
}
});
jQuery(函数(){
var max=10,
计数=100,
s1='ctl00\u cphMainContent\u dlProductList\u ct',
s2=“\u ctl00\u lnkproof下载”,
docID;
对于(var i=计数;i < p>您的<代码> a < /COD>元素在中间有这个(注意两个“ELS”):
…但你的代码> doCID中间有这个(注:“一个和一个EL”):
修复HTML,代码将按原样工作:
然而,这是一种奇怪的编写jQuery的方式
这里有一种不同的方法:
$('a').text(function(i, oldText) {
var num= parseInt(this.id.split('ctl00_cphMainContent_dlProductList_ct')[1]);
if(num>=100 && num<110) {
return oldText === 'Download' ? 'View' : oldText;
}
});
$('a').text(函数(i,oldText){
var num=parseInt(this.id.split('ctl00_cphMainContent_dlProductList_ct')[1]);
如果(num>=100&&num您试图在代码中匹配的元素ID不是DOM中的元素ID
// This is what you want to match
var domID = "#ctl00_cphMainContent_dlProductList_ct100_ctl00_lnkProofDownload"
^ that is an L
// this what your code is trying to match in its first iteration
var docID = "#ctl00_cphMainContent_dlProductList_ct10_ctl00_lnkProofDownload";"
^ that is a 1 (one)
此外,代码的max
变量需要是一个两字符的数字字符串,前导零从零开始,而不是从10开始的整数
就我个人而言,我只想:
// On DomReady...
$(document).ready(function() {
// loop through all anchors that have "lnkProofDownload" in their ID attribute
$('a[id*="lnkProofDownload"]').each(function() {
// and if the text is set to "Download", change it to "View"
if ($(this).text() == "Download") {
$(this).text("View");
}
});
});
你也可以分享生成的html吗?这里有很多错误。首先,你的doc.ready函数在循环中…准备一个提琴,请我不确定@rickhichcock在那里说什么,但你可以在html中有更多内容,显示一整页(或多莱克建议的提琴)是的,我没有注意到1
vsl
,在我的字体中,它们看起来非常相似-d'ohNice catch-l
与1
不同。谢谢Dufmaster33。你的解决方案很有效。我为我的问题措辞不当、HTML中的错误(ctl)以及我对jQuery的理解有限表示歉意(我仍在学习)。你为我节省了不少时间,让我用另一种解决方案来代替。
$('a').text(function(i, oldText) {
var num= parseInt(this.id.split('ctl00_cphMainContent_dlProductList_ct')[1]);
if(num>=100 && num<110) {
return oldText === 'Download' ? 'View' : oldText;
}
});
// This is what you want to match
var domID = "#ctl00_cphMainContent_dlProductList_ct100_ctl00_lnkProofDownload"
^ that is an L
// this what your code is trying to match in its first iteration
var docID = "#ctl00_cphMainContent_dlProductList_ct10_ctl00_lnkProofDownload";"
^ that is a 1 (one)
// On DomReady...
$(document).ready(function() {
// loop through all anchors that have "lnkProofDownload" in their ID attribute
$('a[id*="lnkProofDownload"]').each(function() {
// and if the text is set to "Download", change it to "View"
if ($(this).text() == "Download") {
$(this).text("View");
}
});
});