gpt4 book ai didi

javascript - 如何从 xhtml 页面中获取 jquery 附加和前置数据

转载 作者:行者123 更新时间:2023-12-03 00:51:04 25 4
gpt4 key购买 nike

我试图将多个 html/xhtml/xml 页面加载到网页中。这就是我正在遵循的方法

这是我的基本页面

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">



var lastScrollTop = 0;
var indexup = 4;
var indexdown = 4;
$(window).scroll(function(event){
var st = $(this).scrollTop();
if (st > lastScrollTop){
console.log($(window).scrollTop()+ " <><><> " + $(window).height()+" === "+$(document).height());
// downscroll code
var total = $(window).scrollTop()+$(window).height();
var diff = Math.abs(total-$(document).height());

if(diff<=2) {

// alert("bottom!");
indexdown = indexdown+1;
if(indexdown>7)
{
alert("This is the last page")
}
else
{
appendNext(indexdown);
}
}

} else {
// upscroll code
if ($(window).scrollTop() == 0)
{
console.log($(window).scrollTop() + $(window).height()+" === "+$(document).height());
// alert('Scrolled to Page Top');
indexup = indexup-1;
if(indexup==0)
{
alert('This is the first page');
}
else
{
appendPrevious(indexup);
}

}

}
lastScrollTop = st;
});

function appendNext(index)
{
$.get("html/01.PAGE."+index+".html", function (data) {
$("#appendToThis").append(data);
});

}

function appendPrevious(index)
{
$.get("html/01.PAGE."+index+".html", function (data) {

var old_height = $(document).height(); //store document height before modifications
var old_scroll = $(window).scrollTop();
$("#appendToThis").prepend(data);
$(document).scrollTop(old_scroll + $(document).height() - old_height); //restore "scroll position"

});

}



</script>
</head>
<body>
<div id="loadDiv">

</div>
<script type="text/javascript">

$("#loadDiv").load("xhtml/01.PAGE.4.html");
</script>
</body>
</html>

这按预期工作正常。我首先加载 01.PAGE.4.html,01.PAGE.5.html 附加在 loadDiv 之后,01.PAGE.3.html 附加在前面。

但实际上那些 01.PAGE.4.html 是 01.PAGE.4.xhtml。

我将扩展名从xhtml更改为html进行测试,发现jquery可以加载xhtml代码,但它不能附加或前置相同的代码。

那是当我尝试将代码重新运行为 xhtml 时,代码给了我这个错误

检查控制台时,未捕获的类型错误无法读取 null jquery 的属性“ownerdocument”

enter image description here

这是我的疑问如何从#Document获取值

当我注销它时,它具有整个页面数据,但我不知道如何使用 jquery 附加或前置它。但即使对于xhtml页面,jquery的加载功能也是有效的。请有人帮忙

如何从#Document获取数据以进行jquery追加和前置?

最佳答案

$.get ("xhtml/01.PAGE.xhtml", function (data)
{
$("#appendToThis").html($(data).children());
}, 'xml');

试试这个。请参阅https://api.jquery.com/jquery.get/了解更多详情。

关于javascript - 如何从 xhtml 页面中获取 jquery 附加和前置数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53039649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com