简体   繁体   English

使用jquery在html中查找和替换元素

[英]Find and replace an element in html using jquery

I am trying to load a HTML page into a variable in Jquery, then replace a div element tag in it, so I can get my own id into the div. 我试图将一个HTML页面加载到Jquery中的变量中,然后在其中替换div元素标记,这样我就可以将自己的id添加到div中。 I am using this way to dynamically add more users in my web app, and then do a batch POST to the back end, and put the info into json. 我使用这种方式在我的Web应用程序中动态添加更多用户,然后对后端执行批量POST,并将信息放入json。

Here is my html that I am loading. 这是我正在加载的html。

info.html info.html里

 <div id="user">
     <label>name</label>
     <input type="text" name="name">
     <br>
     <label>email</label>
     <input type="text" name="email">
      <br>
 </div>

I load this with jquery and I want to replace <div id="user"> with something 我用jquery加载它,我想用某些东西替换<div id="user">

like <div id="user 1"> 喜欢<div id="user 1">

I have the following jquery script that has a counter to keep track of what number to append onto the div tag. 我有以下jquery脚本,它有一个计数器来跟踪要附加到div标签的数字。

$(document).ready(function(){
    var make_counter = (function(){
        var count = 0;
        return function(){
            count++;
            return count;
        };
    });
    var _counter = make_counter();
    $("#add").click(function(){
        var counter = _counter();
        var content = $('<div>').load("static/info.html"); //using python flask 
        console.log(typeof(content)); //is of object type
        console.log(typeof(content.html())); //is of type string
        console.log(content.html());//shows up as an empty string
        console.log(content);//prints out the object
        content = content.html().replace('<div id="user ">','<div id="user "'+counter+'>'); // doesn't work. 
        $("#add_div").append(content); //appends correctly if I remove the above line, but all divs have the some id.
        });

});

Any suggestions would be great thanks. 任何建议都会非常感谢。 Also, is the is the best way going about keeping track of how many times a new user is added(as in using a counter and keep track of how they are added, and then do the same when I click submit and create the json)? 此外,这是跟踪新用户添加次数的最佳方式(如使用计数器并跟踪它们的添加方式,然后在单击提交并创建json时执行相同操作) ?

.load() is asynchronous. .load()是异步的。 make your changes in side callback. 在侧面回调中进行更改。

.load("..",function(){
// your changes
});

Also $( "selector" ).load() is the syntax. 另外$( "selector" ).load()是语法。 So create a div and load content to it. 因此,创建一个div并加载内容。 // modified code structure //修改过的代码结构

<div id"content"></div>

$(document).ready(function(){
    var make_counter = (function(){
        var count = 0;
        return function(){
            count++;
            return count;
        };
    });
    var _counter = make_counter();
    $("#add").click(function(){
        var counter = _counter();
        $("#content").load("static/info.html",function(){
             content = $("#content").html().replace('<div id="user ">','<div id="user "'+counter+'>');  
             $("#add_div").append(content); 
}); //using python flask 

        });

});

使用JavaScript,您可以使用.attr()方法来定位id,然后像这样设置其值:

$("#content").attr("id", "user_" + counter);

Try an $.each() function for #user like this: 为#user尝试$ .each()函数,如下所示:

$('#user').each(function(indexNumber){
  $(this).replace('<div id="user'+indexNumber+'"></div>');
});

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

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