所以我有一个充满菜单选项的数组:
var entreescontent=new Array();
entreescontent[0]="Taco Plate";
entreescontent[1]=4.50;
entreescontent[2]="Three tacos of your choice of beef,chicken,pork,or veggie. Served with a side of rice.";
entreescontent[3]="Burrito";
entreescontent[4]=5.50;
entreescontent[5]="Your choice of beef,chicken, pork, or veggie wrapped in a flour tortilla with lettuce, tomato, sour cream, and beans.";
entreescontent[6]="Torta";
entreescontent[7]=4.50;
entreescontent[8]="Your choice of beef,chicken, pork, or veggie in between slices of mexican loaf bread with lettuce, tomato, sour cream, and beans.";
entreescontent[9]="Gorditas";
entreescontent[10]=4.00;
entreescontent[11]="Your choice of beef,chicken, pork, or veggie on top of a corn flour cake with lettuce, tomato, sour cream, and beans.";
当一个人点击"主持人"在我的导航栏上,此功能显示页面上的内容:
function setCourses(entrees){
var k=0;
var element=document.getElementById('contentbody');
element.innerHTML='';
while(k<entrees.length){
var name=entrees[k];
var space=" $<span>";
var price=entrees[k+1];
var break1="</span><br />";
var break2="<br />";
var description=entrees[k+2];
var concat=name+space+price+break1+description+break2+break2;
element.innerHTML+=concat;
k+=3;
}
}
这没有问题,并显示这样的课程:
Taco Plate $ 4.5
您选择的三种玉米饼,牛肉,鸡肉,猪肉或蔬菜。配上一份米饭。
在我的Css中我拥有它,以便当用户将鼠标悬停在价格上(其中附加了span元素)时,价格会变为不同的颜色。 我想要做的是当人们点击该价格时,我希望能够检索该值,以便我可以将其添加到购物车中,我将其作为空白:
<div id='car'></div>.
使用Javascript我该如何尝试这样做?
答案 0 :(得分:0)
这是快速而又脏的东西
<script>
var price = 0;
function add(the_price){
price =+ the_price;
//update value
document.getElementById("car").innerHTML=price;
}
</script>
//from your code
var space=" $<span onclick='add(" + entrees[k+1] + ")'>";
与减号相同......