简体   繁体   中英

jQuery html disappears on refresh

i am modifying a empty <div> with the .html() method of jQuery. When i refresh the page the added code disappears.

$('#div').html($('#div').html() + '<div>'+obj.name+'&nbsp;<a href="">(x)</a>');

Is it possible to change the behaviour or use other methods?

thanks

Edit:

to calify my problem

Maybe it is a design error. It is a form with some fields and a second form where the user can upload files, after a file is uploaded i add the filename and a delete link in a div. If the user submits the form the program connects the created object and the files. It is not really a page where anybody should need to refresh, but it can happen and then the files are saved in the database but the information is lost to connect it with the form object.

Of course, whenever you are reloading the page, all the elements will go back to the original state (as loaded from the server). However there are ways of solving this.

  • Using cookies
  • Using HTML local storage

You have to understand how the client side scripting works. The modification happens on the client's system, not on the server, from where the script is going to load.

you should create a function:

function doMyStuff(){    
     //your code here    
}
doMyStuff();   // that will be triggered on DOM ready // page refresh, whateva'

$('#element').on('click',function(){
    doMyStuff(); // you call your function on click
});

I don't really understand what you're doing here, but you could put the link you want in a #div pa on the HTML page itself. Then if you want to change that line of code when the user clicks a button, you could do that using CSS.

Specifically, like this:

<div>
  <p><a id="example" href="www.example.com">Example</a></p>
</div>

and if say, the user clicks a button (onClick()...)

$('div p a#example').html(//what you want to do here);

That should probably change the code.

That way, your browser doesn't display an empty spot in the HTML itself whilst you wait for the client to make a move first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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