Javascript 替换<;p>;在jQuery中
这是一个jQuery函数,它应该根据所单击的td的类累积一个名为p1或p2的变量。然后,它应该根据类别将其附加到段落Javascript 替换<;p>;在jQuery中,javascript,jquery,html,replace,Javascript,Jquery,Html,Replace,这是一个jQuery函数,它应该根据所单击的td的类累积一个名为p1或p2的变量。然后,它应该根据类别将其附加到段落廉价和高级。写入类的函数位于底部,尽管我觉得这可能不正确,因为段落中的变量可能不会与变量同时更新。另外,底部函数实际上什么都不做 $(document).ready(function () { $('td img').click(function () { if ($(this).parent().hasClass('x')) { alert("Seat
廉价
和高级
。写入类的函数位于底部,尽管我觉得这可能不正确,因为段落中的变量可能不会与变量同时更新。另外,底部函数实际上什么都不做
$(document).ready(function () {
$('td img').click(function () {
if ($(this).parent().hasClass('x')) {
alert("Seat " + ($(this).parent().attr("id")) + " is taken");
} else if ($(this).parent().hasClass('selected')){
$(this).attr('src', 'images/a.gif');
$(this).parent().removeClass('selected');
var z = $(this).parent().attr('id');
$('#'+z+'1').remove();
return false;
} else {
if ($(this).parent().hasClass('n')){
p1=p1+1;
} else if ($(this).parent().hasClass('p')){
p2=p2+1;
}
$(this).attr('src', 'images/c.gif');
$(this).parent().addClass('selected');
alert($(this).parent().attr("class"));
var z = $(this).parent().attr('id');
$('<p>').attr('id', z+'1' ).text(z).appendTo('#order');
return false;
};
});
});
$(document).ready(function(){
$(p1' Standard seats= £'(p1*10)).appendTo('#cheap');
$(p2' Premium seats= £'(p2*20)).appendTo('#premium');
});
您能否测试此代码:
$(document).ready(function () {
var p1=0,
p2=0;
$('#cheap').text(p1 + ' Standard seats= £' + (p1*10));
$('#premium').text(p2 + ' Premium seats= £' + (p2*20));
$('td img').click(function () {
if ($(this).parent().hasClass('x')) {
alert("Seat " + ($(this).parent().attr("id")) + " is taken");
} else if ($(this).parent().hasClass('selected')){
$(this).attr('src', 'images/a.gif');
$(this).parent().removeClass('selected');
var z = $(this).parent().attr('id');
if ($(this).parent().hasClass('n')){
p1--;
} else if ($(this).parent().hasClass('p')){
p2--;
}
$('#'+z+'1').remove();
} else {
if ($(this).parent().hasClass('n')){
p1++;
} else if ($(this).parent().hasClass('p')){
p2++;
}
$(this).attr('src', 'images/c.gif');
$(this).parent().addClass('selected');
alert($(this).parent().attr("class"));
var z = $(this).parent().attr('id');
$('#order').append('<p id="'+z+'1">'+z+'</p>');
};
$('#cheap').text(p1 + ' Standard seats= £' + (p1*10));
$('#premium').text(p2 + ' Premium seats= £' + (p2*20));
return false;
});
});
在click事件中
当一个座位未被选中时,a有这样一个选项:
if ($(this).parent().hasClass('n')){
p1--;
} else if ($(this).parent().hasClass('p')){
p2--;
}
编辑2:
交互代码:
请发布一个完整的代码示例(即HTML),如果可能的话,发布一个JSFIDLE.-1,直到您编辑示例代码以只包含重要的行为止。也修复你的缩进!代码缩进和HTML添加这仍然会每次添加一个新段落,并且它似乎添加的id与添加到order div的id相同,其中td id在上面的代码中替代了我的代码。有和我一样的问题code@Thomas很好,很好。你把我的密码改了什么?看看我的帖子。我加了一些comments@Thomas非常感谢,这是一个很大的帮助!
$('#cheap').text(p1 + ' Standard seats= £' + (p1*10));
$('#premium').text(p2 + ' Premium seats= £' + (p2*20));
if ($(this).parent().hasClass('n')){
p1--;
} else if ($(this).parent().hasClass('p')){
p2--;
}