[英]how do I indicate a javascript generated ID using javascript/jQuery?
I'm counting the number of p tags in a certain div with individual IDs where the p tags and the IDs are randomly generated by php and in varying numbers but in a patterned ID (otherUser1, otherUser2, otherUser3, etc.) 我正在计算某个div中带有单个ID的p标签的数量,其中p标签和ID是由php随机生成的,并且具有不同的数字,但是带有图案ID(otherUser1,otherUser2,otherUser3等)
for (var i = 1; i <= numUsers; i++) {
var tagId = '#otherUser' + i + '';
document.write(tagId);
var userName = $(tagId).html();
document.write(userName);
}
when I document write the userName variable above, it gives me null because just inserting the variable tagId in the jQuery brackets doesn't do the job. 当我的文档写上面的userName变量时,它给我null,因为只是在jQuery括号中插入变量tagId不能完成这项工作。 How do I do this properly?
我该怎么做呢? - I don't mind either using javascript or jQuery, either is fine.
- 我不介意使用javascript或jQuery,也没关系。
I have tried this using jQuery . 我用jQuery尝试过这个。 This is very well working with my localhost.
这非常适合我的localhost。 But, still I don't this will solve your issue or not.
但是,我仍然不会这样解决你的问题。
I have implemented javaScript as : 我已经实现了javaScript:
function submit(){
var numUsers=3;
for (var i = 1; i <= numUsers; i++) {
var tagId = '#otherUser' + i;
var userName = $(tagId).text().toString();
$('#user').append("Username" + i + " : " + userName + "<br/>");
}
}
And that your HTML is : 你的HTML是:
<div id="otherUser1">userName1</div>
<div id="otherUser2">userName2</div>
<div id="otherUser3">userName3</div>
<input type="button" value="Click Me!!" onclick="submit();"/>
<div id="user"></div>
This will display the DIVs currently. 这将显示当前的DIV。 But, in place of this HTML, there would be your PHP auto-generated DIVs, that will not be displayed later.
但是,代替这个HTML,会有你自己生成的PHP自动生成的DIV,以后不再显示。
I have also created this fiddle . 我也创造了这个小提琴 。 I don't know why this is not working even if being the same !!!
我不知道为什么即使是相同的,这也不起作用!!!
Please correct me if I am wrong anywhere. 如果我在任何地方都错了,请纠正我。
Thanking You. 感谢您。
EDIT : perhaps the jsfiddle not supporting the functions, (so removed function line from javaScript) ! 编辑 :也许jsfiddle不支持函数,(从javaScript中删除函数行)! So, updated the fiddle & that is working properly.
所以,更新了小提琴和正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.