var id = 'test';
var dom = $('#loader').clone().load("Views/chatBox.html");
dom.find('span.bn').text(id);
in chatBox.html,there is :
...
<span class="bn">AAA</span>
...
I want to replace "AAA" with "test",but failed(dom.find can't fetch it),which mean it's not available instantly.
How to do it the right way?
It could be by placing a <script> tag (in which case the traditional onload or DOM-readiness solutions will work); or it could be loaded via AJAX or some other means, much later after the DOM is already loaded (so the previously mentioned solutions will never fire).
The jQuery load() method is a simple, but powerful AJAX method. The load() method loads data from a server and puts the returned data into the selected element.
Projects In JavaScript & JQueryjQuery provides a number of methods to manipulate DOM in efficient way. You do not need to write big and complex code to set or get the content of any HTML element.
$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.
If you intend to work with the returned elements, you should do it on the callback function, that is because the retrieving of the HTML is done asynchronously, and the callback function executes when the request has ended and the elements are injected to the DOM :
var id = 'test';
$('#loader').load("Views/chatBox.html", function () {
$('span.bn', this).text(id);
});
Also note that in your example, you were clonning the #loader element, and the cloned element is not in the DOM yet, you will have to insert it, but I'm not sure if you are really wanting to clone the element...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With