如何将URL动态添加到javascript中,以获得第二个页面';要在第一页上显示的s div?

如何将URL动态添加到javascript中,以获得第二个页面';要在第一页上显示的s div?,javascript,jquery,html,dynamic,load,Javascript,Jquery,Html,Dynamic,Load,好的,这就是我的开始代码: $(document).ready(function(){ $('.currentpagediv').load('http://theurl.com/page2/somethingsomewhere.html .secondpagedivclass'); }); 这样做的目的是在当前页面上找到div,,然后添加到第二页(http://theurl.com/page2/somethingsomewhere.html)div 例如,假设我有一个这样的页面: <

好的,这就是我的开始代码:

$(document).ready(function(){
  $('.currentpagediv').load('http://theurl.com/page2/somethingsomewhere.html .secondpagedivclass');
});
这样做的目的是在当前页面上找到div,
,然后添加到第二页(
http://theurl.com/page2/somethingsomewhere.html
)div

例如,假设我有一个这样的页面:

<html>
  <body>
    <div class="currentpagediv">
    </div>
  </body>
</html>
第二页<代码>http://theurl.com/page2/365743668.html:

<html>
  <body>
    <div class="currentpagediv">
    </div>
  </body>
</html>
<html>
  <body>
    <div class="secondpagedivclass">
    </div>
  </body>
</html>
我试过很多方法,但都不管用

以下是我的尝试,但没有成功:


在上面的代码中,我尝试:

  • 获取当前页面的url
  • 将url转换为字符串,以便
  • 修改url,然后
  • 添加函数
    changeURL()
    ,以代替URL在
    加载('


  • 请注意,我想使用jquery这个方法(不是另一种方法)使它工作,因为我也在尝试学习,给我一些完全不同的东西并不能帮助我学习如何使用这个方法。

    您只是缺少字符串串联操作,
    +

    $('.currentpagediv').load(changeURL() + ' .secondpagedivclass');
    
    不要忘记
    .secondpagedivclass
    前面的空格

    changeURL
    函数需要返回结果:

    function changeURL() {
        var theURL = location.pathname;
        var newURL = theURL.replace("/page1/", "/page2/");
        return newURL;
    }
    

    您不需要使用
    .toString()
    ,因为路径名是一个字符串。

    很好,唯一的一点是:
    函数changeURL(){var theURL=location.pathname.toString();URL.replace(“/page1/”,“/page2/”);return;}
    是我的代码,但我只获取此URL而不是新URL:
    http://theurl.com/page1/undefined
    我投票支持你,因为这一部分似乎是正确的,但我还需要修复
    changeURL()
    功能正常。一旦有效,我将选择最佳答案!有效!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!谢谢!谢谢!!!!!!!!!!!!!!!!!!!!!!
    function changeURL() {
            var theURL = location.pathname.toString();
            var newURL = theURL.replace("/page1/", "/page2/");
    }
    $(document).ready(function(){
      $('.currentpagediv').load(changeURL() '.secondpagedivclass');
    });
    
    $('.currentpagediv').load(changeURL() + ' .secondpagedivclass');
    
    function changeURL() {
        var theURL = location.pathname;
        var newURL = theURL.replace("/page1/", "/page2/");
        return newURL;
    }