[英]How do I get jquery to select classes in html which has been loaded using client.open
In my html document I have this code 在我的html文档中,我有这个代码
var client = new XMLHttpRequest();
client.open('GET', '(URL)example.txt');
client.onreadystatechange = function() {
var theblog = client.responseText;
$("#bloglocation").html(theblog);
}
client.send();
});
In that loaded html I have 在那个加载的HTML我有
<p class="example">example</p>
Later on in the file I use jquery to change to color of all the elements in the class example. 稍后在文件中我使用jquery更改为类示例中所有元素的颜色。
$('.example).css({"background-color" : "yellow"});
The jquery works for all the elements with that class that are not in the loaded html. jquery适用于该类中不在加载的html中的所有元素。 How can I get it to work for the class in the loaded html. 如何让它在加载的html中为类工作。
You are using jQuery, so use jQuery: 你正在使用jQuery,所以使用jQuery:
$.get('example.txt').done(function(data) {
$("#bloglocation").html(data);
});
But you need to set the background color after the data has loaded: 但是您需要在加载数据后设置背景颜色:
$.get('example.txt').done(function(data) {
$("#bloglocation").html(data);
$("#bloglocation .example").css({"background-color" : "yellow"});
});
An XMLHttpRequest is an asynchronous operation, meaning the rest of your code is running while that AJAX request is made. XMLHttpRequest是一个异步操作,这意味着在发出AJAX请求时,其余代码正在运行。
You could repeat your code in the onreadystatechange 您可以在onreadystatechange中重复您的代码
client.onreadystatechange = function() {
var theblog = client.responseText;
$("#bloglocation").html(theblog);
$('.example').css({"background-color" : "yellow"});
}
that way the CSS is updated after the AJAX request finishes. 这样,在AJAX请求完成后更新CSS。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.