I'm following this tutorial Drag Drop Inventory in Jquery
and i'm having a problem. In the demo that accompanies the tutorial, the author uses this code :
item.attr('src',drag_item.attr('src')).a开发者_StackOverflowttr('class',drag_item.attr('class')).appendTo(spot).draggable({ revert: 'invalid' });
While this works great in recreating the img element, it does not copy the title attribute( i want to copy that because it's important for my tooltips).
I've changed this code like :
item.attr('title',drag_item.attr('title')).attr('src',drag_item.attr('src')).attr('class',drag_item.attr('class')).appendTo(spot).draggable({ revert: 'invalid' });
which should work i think, but it does not. When i change drag_item.attr('title') to something static like "hello", it works, but the attr func does not return the title.
My html is like :
<div id="inventory" style="width:300px; height:150px; margin:10px; padding:15px; border:1px solid black; display:block; clear:both;">
<% @inv_items.each do |i| %>
<% w = Weapon.find_by_id(i.item_id) %>
<img class='weapons' src= "/images/weapons/<%=h w.imageName %>" title="<%= setWeaponTitleForTooltip(w, i) %>" />
<% end %>
</div>
The html is actually an erb template. Do you have any idea why this happens ?
Are you using Firebug or another debugger to see if there are any errors? The code looks good, but I can't imagine the resulting HTML of your erb-template. Maybe the resulting title-attribute of your image tags are empty.
Yours code seems to be fine. working demo
i.e. derived from example given in above link. It is also copying title attribute.
精彩评论