Jquery 从html中删除选定的类
我想用jQuery加载html代码并从代码中删除一些类,我的代码是:Jquery 从html中删除选定的类,jquery,html,removeclass,Jquery,Html,Removeclass,我想用jQuery加载html代码并从代码中删除一些类,我的代码是: <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <div id="new-nav"></div>
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div id="new-nav"></div>
<script>
$("#new-nav").load("/php/grab.php");
</script>
<script>
$('.main_title_news_submain_block').remove();
</script>
</body>
</html>
$(“#new nav”).load(“/php/grab.php”);
$('.main_title_news_submain_block').remove();
问题是。删除功能不工作,我不知道为什么 将其包装在
document.ready
中,前提是该html元素在页面加载之后
$(function () {
$('.main_title_news_submain_block').remove();
});
如果使用.load
加载,则将其添加到回调中
$("#new-nav").load("/php/grab.php", function () {
$('.main_title_news_submain_block').remove();
});
您是否在方法的回调中尝试了它 如果提供了“完整”回调,则在 已执行后处理和HTML插入
因此,在加载函数将内容加载到DOM中指定的div之后,将执行带有
remove()
方法的行。在使用回调函数加载内容之后,需要执行.remove()
$("#new-nav").load("/php/grab.php", function(){
/*removes the element with class main_title_news_submain_block*/
$('.main_title_news_submain_block').remove();
});
编辑:
您还可以将所有内容包装在一个文档中,以确保在dom中加载id为new nav的元素后加载
$(document).ready(function(){
$("#new-nav").load("/php/grab.php", function(){
/*removes the element with class main_title_news_submain_block*/
$('.main_title_news_submain_block').remove();
});
});
我发现您没有在任何地方使用DOM就绪函数。确保你在那里写下你的脚本,这样你就看不到任何异常
<script type="text/javascript">
$(function() {
$("#new-nav").load("/php/grab.php" , function() {
$('.main_title_news_submain_block').remove();
});
});
</script>
$(函数(){
$(“#new nav”).load(“/php/grab.php”,function()){
$('.main_title_news_submain_block').remove();
});
});
我不知道为什么,但类仍然被删除;(@TomasLietuva-什么意思“类仍然被移除”?您试图完成什么?删除类或附加到类的元素?不,抱歉,仍然显示类。您可以提供添加到新nav元素的html吗?您确定在该html中有一个带有类主标题新闻子主块的元素吗?是的,我不明白怎么做代码必须看起来,只写第二个脚本或什么?@TomasLietuva我想是的..但在此之前,请尝试理解您是如何以及为什么这样做的。您在示例p.s.中也没有使用ready函数。无法使用$(function(){是$(document)的别名。ready(function()){
<script type="text/javascript">
$(function() {
$("#new-nav").load("/php/grab.php" , function() {
$('.main_title_news_submain_block').remove();
});
});
</script>