如何为下面的代码编写click事件?
var prodName = ["Phlips camera","HP keyboard","iPad","iPhone","Dell Mouse","shirts","books","samsung mobiles","samsung TV", "Phlips TV"," HP Mouse","iPad Charger","iPhone Charger","Dell Keyboard"," T shirts"," Ebooks"];
var cartVal = [];
//add to cart
$('#addcart').live("click",function()
{
alert( prodName[id]+" Item Added");
cartVal[item_count] = id;
//alert("Item Count :::::" + item_count + " Selected Id:::: " + id);
item_count++;
});
//create a list to display the selected product in the cart.
for(var l=0 ;l< cartVal.length; l++)
{
//alert(" name ::::"+ prodName[cartVal[l]]);
var listItem = document.createElement('li');
listItem.setAttribute('id','listitem_'+ l);
listItem.setAttribute('data-icon','false');
listItem.setAttribute('data-theme','c');
listItem.innerHTML = "<a href='#' data-role='button' data-theme ='c' id='" + cartVal[l] + "' rel='external' data-inline='true' style='margin-left:1em;'> <font size='2'>"+ prodName[cartVal[l]] + "</font><span id='viewPage' class='ui-li-count'>View</span></a> <a href='#' id='delete' data-role='button' data-rel='dialog' data-transition='slideup'> Purchase album</a>";
parent.appendChild(listItem);
}
这里我想为锚标记<a>
编写两个点击事件..如果我点击查看或产品名称,它会显示详细信息,然后我点击删除图标它会删除产品列表...例如:购物清单中有5个项目我要删除两三个产品吗?
答案 0 :(得分:1)
U可以在on tag中添加以下功能以进行id delete
<a href='#' id='delete' **onclick=removeCurrentItem(event)** data-role='button' data-rel='dialog' data-transition='slideup'> Purchase album</a>
function removeCurrentItem(event){
var tar_ele;
if ($.browser.msie) {
// IE takes SRCELEMENT for event source.
// may the force shred IE.
tar_ele = $(e.srcElement);
} else {
tar_ele = $(e.target);
}
// remove the current Li tag
tar_ele.parent().find('li').remove();
}
类似于View span你可以拥有以下代码
<a href='#' data-role='button' data-theme ='c' id='" + cartVal[l] + "' rel='external' data-inline='true' style='margin-left:1em;'> <font size='2'>"+ prodName[cartVal[l]] + "</font><span id='viewPage' class='ui-li-count' **onclick=showDetails(event)**>View</span></a>
showDetails函数将显示产品的详细信息。 对于前。
function showDetails (event){
var tar_ele;
if ($.browser.msie) {
// IE takes SRCELEMENT for event source.
// may the force shred IE.
tar_ele = $(e.srcElement);
} else {
tar_ele = $(e.target);
}
// remove the current Li tag
var id = tar_ele.parent('a').attr("id");
// NOW do the requisiste display using the ID.
}