I've got a button which looks like this:

<a class="buttonS bDefault tipN" id="customerBasket" original-title="<ul class='shoppingBasket'></ul>" href="#"><span class="icos-cart3"></span></a>

Its a tooltip and I want to append list-items to the class shoppingBasket.

I've tried by doing the following:

$('.shoppingBasket ul').append(<li><span class=\'basketPic\'>Test</span><span class=\'basketName\'>Test123</span></li>);

But I get the error:

SyntaxError: Unexpected token '<'

Any ideas what I'm doing wrong here?

有帮助吗?

解决方案

You forgot your quotes:

$('ul.shoppingBasket').append("<li><span class='basketPic'>Test</span><span class='basketName'>Test123</span></li>");

EDIT after getting more info:

You're wanting to update an attribute, which is more complicated than just appending something to a dom element. But, you should be able to do it. Try something like this:

$button_title = $('.buttonS').attr('original-title');
$('body').append('<div id="cart-holder" style="display:none;">' + $button_title + '</div>');
$('#cart-holder ul.shoppingBasket').append('<li><span class=\'basketPic\'>Test</span><span class=\'basketName\'>Test123</span></li>');
$('.buttonS').attr('original-title', $('#cart-holder').html());
$('#cart-holder').remove();

This will add a div to the body with the HTML in original-title, append your HTML to it, set the original-title to it, then remove the element it just made. You'll likely have to tweak it to get the effect you want.

Fiddle

其他提示

Please note that the append() method accepts a string. And you should always add a string to its parameter. Otherwise it won't work for you and you'll get this error.

append("<li><span class='basketPic'>Test</span><span class='basketName'>Test123</span></li>")

This would do it. Otherwise, it won't work.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top