【发布时间】:2013-10-18 21:07:32
【问题描述】:
下面是工作脚本。
<script type="text/javascript">
window.lang = new jquery_lang_js();
$(document).ready(function() {
window.lang.run();
$(".jp").click(function (e) {
e.preventDefault();
var $this = $(this);
var id = $this.attr('href');
window.lang.change('jp');
});
$(".en").click(function (e) {
e.preventDefault();
var $this = $(this);
var id = $this.attr('href');
window.lang.change('en');
});
});
</script>
<a href="#" class="en">Switch to English</a>
<a href="#" class="jp">Switch to Japanese</a>
//////////////////////////////////
现在,在快速告诉我“如果它有效的话有什么问题?”我还有几个课程要添加,所以我试图摆脱冗余。我知道如何将 href 与相同的函数结合起来,但在这里,每个类都有可变函数,我自己无法弄清楚。
这是我迄今为止尝试过的。
<script type="text/javascript">
window.lang = new jquery_lang_js();
$(document).ready(function() {
window.lang.run();
$this.each(function(){
$("a").click(function (e) {
e.preventDefault();
// var $this = $(this);
var id = $this.attr('href');
window.lang.change('jp');
window.lang.change('en');
});
});
});
</script>
这是行不通的。
并且评论说我应该先学习 jQuery 的基础知识然后在这里提问只是因为我不能替换 var $this = $(this); 是不是有点苛刻。到 $this.each(function(){ 正确吗?
我是 jQuery 新手,我想通过在这里提问来学习。说我应该先学习基础知识并不是很有建设性。如果你没有帮助那么好,但所有这些批评是为了什么?
【问题讨论】:
-
这可能更适合codereview.stackexchange.com。
-
如果用
$(this)代替$this有问题,我建议你退后一步,开始学习JavaScript/jQuery 的基础知识。 -
他现在可以学习了!为什么要把他送走..?
var $this=$(this)如果您在该范围内不止一次需要它,它会让事情变得更好。所以你是对的,在你的代码中你不需要它看起来像。 -
这是最好的 jQuery 资源顺便说一句,玩得开心^^ tutsplus.com/course/30-days-to-learn-jquery