简体   繁体   English

jQuery .Load但不是替换前缀

[英]Jquery .Load but instead of replace prepend

I am trying to load in some text from another page, however I want to prepend it rather than replace. 我正在尝试从另一页加载一些文本,但是我想在它前面添加而不是替换。

So at the moment I have: 所以目前我有:

$('#01-L1-00-Container').load('Page.aspx #Q-01-L1-00');

But '01-L1-00-Container' includes some content I need to keep and cannot overwrite. 但是'01 -L1-00-Container'包含一些我需要保留且不能覆盖的内容。 How do I prepend instead of replace? 我该如何代替替换?

You can either do this: 您可以执行以下操作:

$("#01-L1-00-Container").prepend( $("<div>").load("Page.aspx #Q-01-L1-00"));

Or 要么

$("<div>").load("Page.aspx #Q-01-L1-00", function() {
      $("#01-L1-00-Container").prepend($(this).find("#Q-01-L1-00").html());
});

These both work by creating a div in the dom, loading the new page (with ajax) into that div and then prepending the newly created div to the container. 这两种方法都是通过在dom中创建一个div,然后将新页面(使用ajax)加载到该div中 ,然后将新创建的div放在容器之前。

Assuming you don't want to append the data to some other element inside the container, you can't do this with .load alone, but it is easy: 假设您不想将数据附加到容器内的其他元素,则不能仅使用.load来做到这一点,但这很容易:

$.get('Page.aspx').done(function (html) {
   $(html).find('#Q-01-L1-00').prependTo("#01-L1-00-Container');
});

Try this instead : 试试这个代替:

$("#01-L1-00-Container").prepend($("<div />").load("URL of Page"));

Hope this will help !! 希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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