简体   繁体   English

jQuery .html和.load

[英]Jquery .html and .load

I'm trying to teach myself jQuery and I'm a little stomped with the load() method. 我正在尝试自学jQuery,但对load()方法感到有些不知所措。 I'm working on eBay listings. 我正在处理eBay列表。 Yes, I know includes are not allowed on ebay. 是的,我知道eBay上不允许包含。 However, there is a workaround that has been around for a few years and ebay doesn't seem to be cracking down on it. 但是,有一种解决方法已经存在了几年,而ebay似乎并没有对此进行严厉打击。

var ebayItemID='xxxxxxxxxxxxxx'; // This is eBay code. I cannot edit it. 

<h1 id="title"> TO BE REPLACED</h1>

$(document).ready(function(){
var link = "http://www.ebay.com/itm/" + ebayItemID + "?item=" + ebayItemID +   &viewitem=&vxp=mtr";
var newTitle = $('#title').load(link + "#itemTitle");
$('#title').html(newTitle);
});

What's the point of this. 这有什么意义。 I want to show the item title on the description, but I want to do so dynamically, 我想在说明中显示项目标题,但我想动态地显示,

  1. load will not work on different domains (ebay on your case) 加载将无法在不同的域上工作(在您的案例中为ebay)
  2. load will set the content directly to your element. 加载会将内容直接设置为您的元素。 You can't assign it to a var. 您不能将其分配给var。
  3. If you would like to indicate you want to extract content from a specific element you need to add a space between your link and the element id: 如果您想表明要从特定元素中提取内容,则需要在链接和元素ID之间添加一个空格:

You can find more info on the jQuery docs 您可以在jQuery文档中找到更多信息

$('#title').load(link  + ' #itemTitle', function() {
  alert('Load was performed.');
});

When you use load will place the returned html into the element(this case #title ). 使用load会将返回的html放入元素(本例为#title )。
So you don't need to call html after it. 因此,您无需在其后调用html

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

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