【问题标题】:jQuery doubling up root path on a load() functionjQuery在load()函数上加倍根路径
【发布时间】:2016-06-24 14:48:59
【问题描述】:

为什么我的根路径在这个函数中被加倍了?

function bodyClass(id) {
   var catName = $('#location').children().eq(4).text(),
      rootPar = ("http://localhost/site/" + catName),
      banner = $('#wantedDiv'),
       
      // this load variable doubles up the root path and does not escape the first single quote 
      content = $('<div />').load("'" + rootPar + " " + banner + "'", function(){ 
         $('#placeDiv').prepend(content.html());
      });
      
      // console logging retrieves the correct path inclusing the added "catName"
      console.log(rootPar);

}
bodyClass();

这是浏览器控制台向我显示的内容:

console.log:http://localhost/site/page

load() 函数: GET http://localhost/site/'http://localhost/site/page' 404(未找到)

【问题讨论】:

    标签: jquery function url load


    【解决方案1】:

    当页面加载时,它将加载到http://localhost/site/,因此通过再次添加根网页,它会将其附加到已经存在的网页中。我只想添加catName 而不是rootPar

    【讨论】:

    • 我已经尝试过这样做,但没有运气。这是我得到的错误 GET localhost/site/'Page 404 (Not Found) -> 仍然没有转义第一个单引号。
    • 如果完全删除单引号会怎样?
    • 我得到一个不同的错误 -> 未捕获的语法错误,无法识别的表达式:[object Object]'
    • 好的,所以如果我这样做是通过/工作 -> .load(rootPar + ' ' + '#wantedDiv', function(){... -> 我只需要弄清楚如何调用 ID 变量而不是实际的 ID 名称。
    【解决方案2】:

    想通了...对于任何寻找类似解决方案的人。 只是将串联移动到变量中,而不是在 load() 函数中。

    function bodyClass(id) {
       var catName = $('#location').children().eq(4).text(),
          rootPar = (catName + "/ "),
          banner = "'" + '#' + 'wantedDiv' + "'",
          content = $('<div />').load(rootPar + banner, function(){ 
             $('#placeDiv').prepend(content.html());
          });
    
    }
    bodyClass();

    【讨论】:

      猜你喜欢
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 2014-04-18
      • 2012-08-31
      相关资源
      最近更新 更多