我正在尝试学习jQuery的基础知识,并在我想要添加功能的一个站点上进行测试,以便onClick能够改变超链接的内部文本和href,我的问题是,当我单击onClick事件时,会发生更改,但是onClick按钮会消失,而我尝试更改的onClick会消失,但我的.html功能有效吗?
用于onClick的HTML -
<div class="container" style="margin-top: -20px;">
<div class="gamesdesc">
<h4 id="gquote" style="padding: 5px 5px 5px 5px;">test<br><br>
<ol class="carousel-indicators">
<li data-target="#myCarousel" onClick="$('#my_div').cod4();" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" onClick="$('#my_div').csgo();" data-slide-to="1" class="active"></li>
<li data-target="#myCarousel" onClick="$('#my_div').tf2();" data-slide-to="2" class="active"></li>
</ol>
<a id="glinks" href=""><btn class="btn btn-warning">Test</btn></a></h4> jQuery -
(function ($) {
$.fn.cod4 = function () {
$("#gquote").html("Test");
$("glinks").attr("href", "cod4");
};
})(jQuery);
(function ($) {
$.fn.tf2 = function () {
$("#gquote").html("Test");
$("glinks").attr("href", "csgo");
};
})(jQuery);
(function ($) {
$.fn.csgo = function () {
$("#gquote").html("Test");
$("glinks").attr("href", "tf2");
};
})(jQuery);发布于 2013-09-01 12:27:54
这使得元素显示为disapper,因为您正在更改gquote的html,即包含所有按钮的h4。这意味着gquote中的所有内容都消失了,现在它包含了文本Test。要么使用.append()而不是html(),要么更改不同元素的html。
如果你忘了#在$("glinks")
https://stackoverflow.com/questions/18558397
复制相似问题