在选择器不起作用的情况下调用

在选择器不起作用的情况下调用

本文介绍了在选择器不起作用的情况下调用.load()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对该主题进行了相当多的研究,但我只是想不出为什么它没有按预期的方式工作.我有一个表单,用户用Amazon产品ID(ASIN)填写,然后将该产品ID转换为URL,然后在页面上.load()将该URL转换.问题是,当我将选择器与.load()一起使用时,仅加载页面的一部分,而.load()则不执行任何操作.

I've done a fair bit of research on the subject and I just can't figure out why this isn't working the way it should. I have a form that a user fills out with an Amazon product ID (ASIN), and then I convert that product ID into a URL and then .load() that URL on my page. The problem is that when I use a selector with .load() to load just a portion of the page, the .load() doesn't do anything.

当我仅使用URL删除选择器和.load()时,.load()起作用.这是我的代码:

When I remove the selector and .load() with just the URL, .load() works. Here is my code:

<div id = "html" style = "display: none;"></div>
<input id = "url" type = "text" size = "100"></input>
<button id = "load">Load</button>

<script type="text/javascript">
var theURL;

$('#load').on('click', function() {
    if ($('#url').val().length < 14)
        theURL = 'http://www.amazon.com/dp/' + $('#url').val() + ' #dp';
    else
        theURL = $('#url').val() + ' #dp';
    $('#html').load(theURL);
});
</script>

例如,theURL将是http://www.amazon.com/dp/B00J4GIRH4 #dp,当我在URL中使用#dp选择器时.load()不会提取任何内容(通过调用.load()后检查#html元素进行验证).删除#dp选择器并且theURLhttp://www.amazon.com/dp/B00J4GIRH4时,.load()可以正常工作.

So for example, theURL would be http://www.amazon.com/dp/B00J4GIRH4 #dp, and when I have the #dp selector in the URL .load() won't pull anything (verified by checking the #html element after invoking .load()). When the #dp selector is removed and theURL is http://www.amazon.com/dp/B00J4GIRH4, .load() works fine.

推荐答案

id="dp"在Amazon页面的body元素上.提供给load()#引用将搜索已加载面板的后代.不是body元素本身.

The id="dp" is on the body element in the Amazon page. The # reference supplied to load() will search the descendants of the loaded panel. Not the body element itself.

尝试定位子元素,或者在默认情况下返回body时忘记选择器.

Try target a child element, or forget the selector as you get body back by default.

这篇关于在选择器不起作用的情况下调用.load()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:57