jQuery停止加载当前域URL

时间:2019-04-03 11:54:34

标签: jquery html

我要存储页面上每个元素内链接的网址,并在每个要定位的元素中插入一个新的div:

$('.element a').each(function(){
    var storePLU = $(this).attr('href');
    $('<div class="newDivs"></div>').insertBefore(this);
});

然后,我正在加载存在于同一域的不同页面上的元素,如果我手动指定页面网址,该元素可以正常工作:

$('.newDivs').load('/product/productName/  + #targetDivOnOtherPage'); 
/* 
This returns the correct elements from:
https://example.com/product/productName/stock 
*/

但是,如果我尝试用存储在storePLU变量中的网址填充负载的网址部分:

$('.newDivs').load("'" + storePLU + " " + "#targetDivOnOtherPage'");
/* 
This returns an error as even though the variable storePLU would return:
/product/productName/ which is correct
However when used in this .load() it tries to load:
https://example.com/category'https://example.com/product/productName/ 
*/

我希望它不要在当前URL之前添加预期URL的前缀。我认为问题在于期望在开头看到撇号和预期的url,但是它以引号开头,因此只将当前的url作为默认位置添加在第一位。

我该如何工作?

当前完整代码:

$('.element a').each(function(){
    var storePLU = $(this).attr('href');
    $('<div class="newDivs"></div>').insertBefore(this);
    $('.newDivs').load("'" + storePLU + " " + "#targetDivOnOtherPage'");
});

1 个答案:

答案 0 :(得分:1)

您正在使用各种不必要的引号。您要做的只是简单的字符串连接,以便传递给load()的最后一个字符串是一个有效的URL和空格后的选择器

尝试

$('.newDivs').load(storePLU + " #targetDivOnOtherPage");
相关问题