简体   繁体   English

如何使用javascript / jQuery指示javascript生成的ID?

[英]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.

相关问题 如何使用javascript或jQuery在标签内使用已知的html内容查找标签的ID? - How do I look for a tag's ID using the known html content inside the tag using javascript or jQuery? 如何使用BeautifulSoup抓取用javascript生成的数据? - How do I scrape data generated with javascript using BeautifulSoup? 如果输入标签中的ID由php / mysql输出生成,如何将ID传递给JavaScript(getElementByID)? - How do I pass an Id into JavaScript (getElementByID) if the id in my input tag is generated by php/mysql output? jQuery / javascript如何获取对象的ID? - JQuery/javascript how do I get the ID of an object? 如何使用jQuery或Javascript隐藏具有相同ID的所有表行? - How do I hide all table rows with the same id using jQuery or Javascript? javascript / jquery指示某物为变量 - javascript/jquery indicate something is a variable 如何使用其ID在jquery / javascript中选择动态生成的表单元素? - How to select a dynamically generated form element in jquery / javascript using its ID? 如何使用jquery或javascript获取tabcontainer ID? - How can I get a tabcontainer ID using jquery or javascript? 如何使用Java脚本在ID中设置标签样式? - How do I style a tag within an ID using Javascript? 如何在IE和Safari中使用Javascript检测按钮的ID? - How do I detect the ID of a button using Javascript in IE and Safari?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM