简体   繁体   English

Jquery从另一个页面添加HTML

[英]Jquery add HTML from another page

I want to use jQuery to add a number to the end of a navigation menu list item. 我想使用jQuery在导航菜单列表项的末尾添加一个数字。 This is the number of products on another page. 这是另一页上的产品数量。 Currently a single list item looks something like this: 目前单个列表项看起来像这样:

<li class="ui-accordion ui-widget ui-helper-reset" role="tablist">
<a title="New In" href="http://example.com/shop/clothing/new-in">New In</a>
</li>

However I want to add the number of items directly after 'New' for example 'New (20)'. 但是我想在'New'之后直接添加项目数,例如'New(20)'。 This number is located on items href, in the following place on that page: 此编号位于项目href上,位于该页面的以下位置:

<div class="paging">
<span class="itemcount">174</span>
</div>

Each list item has a different href but the same class which contains the unique number that I need. 每个列表项都有不同的href,但是包含我需要的唯一编号的同一个类。 Any ideas? 有任何想法吗?

I have tried: 我努力了:

$("#ui-id-12 > .ui-accordion > li:eq(0) > a:eq(0)").replaceWith('<a title="New In" href="http://example.com/shop/clothing/new-in">New In ' + $(this).load("http://m.example.com/shop/clothing/new-in.html .itemcount") + '</a>');

Here is a simple way for you to get the value you are looking for: 这是一种获取您正在寻找的价值的简单方法:

$(this).load("http://m.example.com/shop/clothing/new-in.html", function(data){
  alert(data.match(/<span class="itemcount">(\d+)<\/span>/)[1]);
  $("#span-id").text(data.match(/<span class="itemcount">(\d+)<\/span>/)[1]);
  // for putting stuff into a <span> :)
});

You might want to make a <span> with a specific ID to put the loaded content into like so: 您可能希望使用特定ID创建<span>,以将加载的内容放入:

<a title="New In" href="http://example.com/shop/clothing/new-in">New In <span id="span-id"></span></a>

查找.append()在任何带有结束标记的html标记中添加craps

$('a').append("123");

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

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