问题描述
我对Jquery真的很陌生,正试图弄清楚如何确定哪个链接被点击,然后运行if else语句。这是一个视频游戏粉丝。
我有一个表格,用于设置单位统计数据和一些Divs设置为按钮(链接显示设置为阻止),单击按钮时会填充表格与该单位的数据和图像。我的工作正常。现在在某些情况下,一个单位拥有不同属性的替代武器。我有一个带链接的空白tr标签,当单位有一个替代武器时,它只能在td标签中获得文本。
我所要做的是将链接替换(切换)武器统计数据,而不用离开或刷新页面。我得到它与一个单位的工作,但它是交替的表格数据(HTML)的每个单位,具有该特定链接出现相同的方式。我需要它来知道哪个按钮被点击,然后当alt链接出现时,它会切换该单位的适当武器数据。
我真的希望这是有道理的,因为这只是我第二天与jquery合作。请尽可能详细,因为我是一个新手。非常感谢
我的html设置是。
< div id =unit1>< a href =#> Unit 1< / a>< / div>
< div id =Unit2>< a href =#> Unit 2< / a>< / div>
< div id =Unit3>< a href =#> Unit 3< / a>< / div>
< table>
< tr>
< td>< a id =altweaponhref =#>< / a>< / td>
< td>< span id =weapName>< / span>< / td>
< td>< span id =attack>< / span>< / td>
< td>< span id =range>< / span>< / td>
< / tr>
< / table>
我的jquery设置完成。
<$ p $点击(功能(){
$(#altweapon)。html();
$(#weaponsName ).html(weapon 1);
$(#attack)。html(10);
});
$(#Unit2)。click(function(){
$(#altweapon)。html(Alternate);
$(#weapName)。html (武器1);
$(#attack)。html(5);
});
$(#Unit3)。click(function(){
$(#altweapon)。html(Alternate);
$(#weapName)。html (武器1);
$(#attack)。html(15);
});
$(#altweapon)。click(function(){
$(#altweapon)。toggleClass(Wp1)。toggleClass(Wp2);
$ b $ if if $(#altweapon)。hasClass(Wp1))
{
$(#weapon)。html(Weapon 1);
$ b $ else
$(#weapon)。html(Weapon2);
}
});
不知道这是否回答您的问题,但看起来无论你在做什么都可以从你学习jquery 中受益。有了这个,你可以存储和检索设置。例如...
< a class =weapondata-attack =10data-range =1 > Unit 1< / a>
< a class =weapondata-attack =12data-range =2> Unit 1< / a>
< a class =weapondata-attack =15data-range =3> Unit 1< / a>
然后jQuery ...
$ p $ $('a.weapon')。click(function({
$ ('#weapName')。html($(this).html());
$('#attack')。html($(this).data('attack'));
$('#range')。html($(this).data('range'));
});
您也可以设置适用于整个文档的 $('body')。data('foo',52);
不只是一个特定的元素......
I'm really new to Jquery and am trying to figure out how to determine which link was clicked and then run a if else statement. This is for a video game fansite.
I have a table set up for unit stats and bunch of Divs set up as buttons (link with display set to block), when a button is clicked it populates the table with that units data and images. I have that working properly. Now in some cases a unit has an alternate weapon with different stats. I have a blank tr tag with a Link inside that only gets text in the td tag when the unit has an alternate weapon.
What I'm looking to do is have that link alternate (toggle) weapon stats without leaving or refreshing the page. I got it working with one unit but it's alternating the table data (html) the same way for every unit that has that specific link appear. I need it to know which button was clicked and then when the alt link appears, it toggles the proper weapon data for that unit.
I really hope this makes sense because this is only my second day working with jquery. please be as specific as you can with help since I'm a newbie. Thanks so much
My html set up is.
<div id="unit1"><a href="#">Unit 1</a></div>
<div id="Unit2"><a href="#">Unit 2</a></div>
<div id="Unit3"><a href="#">Unit 3</a></div>
<table>
<tr>
<td><a id="altweapon" href="#"></a></td>
<td><span id="weapName"></span></td>
<td><span id="attack"></span></td>
<td><span id="range"></span></td>
</tr>
</table>
My jquery set up.
$("#Unit1").click(function(){
$("#altweapon").html("");
$("#weapName").html("weapon 1");
$("#attack").html("10");
});
$("#Unit2").click(function(){
$("#altweapon").html("Alternate");
$("#weapName").html("weapon 1");
$("#attack").html("5");
});
$("#Unit3").click(function(){
$("#altweapon").html("Alternate");
$("#weapName").html("weapon 1");
$("#attack").html("15");
});
$("#altweapon").click(function() {
$("#altweapon").toggleClass("Wp1").toggleClass("Wp2");
if ($("#altweapon").hasClass("Wp1"))
{
$("#weapon").html("Weapon 1");
} else
{
$("#weapon").html("Weapon2");
}
});
Not sure if this answers your question or not, but it seems that whatever you are doing could benefit from you learning about jquery data objects. With this, you could store and retrieve setting. For example ...
<a class="weapon" data-attack="10" data-range="1">Unit 1</a>
<a class="weapon" data-attack="12" data-range="2">Unit 1</a>
<a class="weapon" data-attack="15" data-range="3">Unit 1</a>
Then jQuery...
$('a.weapon').click(function({
$('#weapName').html( $(this).html() );
$('#attack').html( $(this).data('attack') );
$('#range').html( $(this).data('range') );
});
You can also set $('body').data('foo', 52);
that would apply to the whole document, not just a particular element...
这篇关于Jquery查找哪个按钮被点击然后运行一个特定的If else语句? (新手)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!