简体   繁体   English

.unwrap() 在 .append().load() 之后不起作用

[英].unwrap() doesn't work after .append().load()

I am currently doing this:我目前正在这样做:

$("div#js-product-list .products.row").append($("<div>").load('http://andreisdev.tk/alexstan/en/2-home?page=' + page_nr + ' .products.row article.product-miniature.js-product-miniature'));

Then I am incrementing the page_nr , that is not important in this question because it works.然后我增加page_nr ,这在这个问题中并不重要,因为它有效。

After that I am trying to unwrap the article s just loaded in the page to make them siblings with the other article s already in the page, since load overrides the selection you make, I had to make that div at the end of all my current articles.在那之后,我试图解开刚刚加载到页面中的article ,使它们与页面中已经存在的其他article成为兄弟,因为加载覆盖了您所做的选择,我不得不在所有当前的末尾创建该div文章。

I am trying to unwrap them this way:我试图以这种方式打开它们:

$('article.product-miniature.js-product-miniature ~ div article.product-miniature.js-product-miniature').unwrap();

So basically I am selecting the div just appended as sibling to the current article s I had then I'm selecting the article s just loaded inside that new div and trying to unwrap them.所以基本上我选择刚刚附加为当前article的兄弟的 div 然后我选择刚刚加载到新 div 中的article并尝试解开它们。

If I am doing this in the console, first typing the command with .append().load() and run it, then the .unwrap() one it works totally fine, if I am inserting and runnning them both at the same time though it does not work, same thing that happens from my .js file.如果我在控制台中执行此操作,首先使用.append().load()键入命令并运行它,然后使用.unwrap()一个它工作得很好,如果我同时插入和运行它们虽然它不起作用,但我的 .js 文件也发生了同样的事情。

I kind of succeded with:我有点成功:

setTimeout(function() {
  $('article.product-miniature.js-product-miniature ~ div article.product-miniature.js-product-miniature').unwrap(); 
}, 500);

But there has to be a direct way of doing it.但是必须有一种直接的方法来做到这一点。

EDIT !!!

Since the comments won't be seen by too many, here's the response:由于评论不会被太多人看到,这里是回应:

$("div#js-product-list .products.row").append($("<div>").load(''+link+' .products.row article.product-miniature.js-product-miniature', function() { $('article.product-miniature.js-product-miniature ~ div article.product-miniature.js-product-miniature').unwrap(); }));

The issue is because load() is asynchronous, so you're attempting to unwrap elements that don't exist yet.问题是因为load()是异步的,所以您试图解开尚不存在的元素。 This is why using the timeout to delay the logic works.这就是使用超时来延迟逻辑工作的原因。

A better solution is to use the callback argument of load() to provide a function to execute your unwrap logic after the content has been added to the DOM.更好的解决方案是使用load()的回调参数来提供一个函数来将内容添加到 DOM执行解包逻辑。 Like this:像这样:

$("div#js-product-list .products.row").append($("<div>").load('http://andreisdev.tk/alexstan/en/2-home?page=' + page_nr + ' .products.row article.product-miniature.js-product-miniature', function() {
  $('article.product-miniature.js-product-miniature ~ div article.product-miniature.js-product-miniature').unwrap();
}));

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

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