简体   繁体   English

jQuery此串联

[英]Jquery this concatenation

first off i am a beginer in coding so please bear with me. 首先,我是编码方面的初学者,所以请多多包涵。 i have several images on a page forming a navigation bar that will be animated. 我在页面上有几张图像,形成了一个导航栏,将对其进行动画处理。 i am trying to move an element based on what element is moused over. 我试图根据鼠标悬停在哪个元素上移动元素。 all the images have unique ids and share common names, ie the names for the home icons are "home" and "homeShad". 所有图像都具有唯一的ID,并具有相同的名称,即主页图标的名称为“ home”和“ homeShad”。 the idea is when you mouse over home, homeShad will move and being as i have several links im trying to slim my code down. 这个想法是当您将鼠标悬停在首页上时,homeShad将会移动,并且由于我有多个链接正在尝试缩小我的代码。 this has led me to here and what im trying to do 这导致我来到这里,而我正试图去做

$.reset = function()  {
$(this + 'Shad').animate({
    left: '+=100'}, 300)

$('#home').mouseenter(function() {
    $.reset
    })

this doesnt work so i tried this 这不起作用,所以我尝试了这个

$.reset = function()  {
$('#'+ $(this).attr("id")+" shad").animate({
    left: '+=100'}, 300)

and still no joy. 仍然没有喜悦。

('#home').click(function() {
    alert('#'+ this.id + 'shad')
})

this managed to get the alert to show "#homeShad" exactly what im looking for when i tried to animate it there was no joy 这设法使警报显示“ #homeShad”,而当我尝试对其进行动画处理时,我正在寻找的正是我没有的乐趣

hopeing some one can help and i am sorry if i made any mistakes in posting here 希望有人能为您提供帮助,如果我在此处张贴任何错误,我感到抱歉

Check out this fiddle: http://jsfiddle.net/eVDe3/ 看看这个小提琴: http : //jsfiddle.net/eVDe3/

Hope this answers your question! 希望这能回答您的问题! Your code should have been: 您的代码应该是:

$('#home').click(function() {
    $('#'+this.id+'Shad').animate({
        left: '+=100'
    },300);
});

EDIT: If you want to call it by a function, you have to pass $(this).id . 编辑:如果要通过函数调用它,则必须传递$(this).id Check this updated fiddle here: http://jsfiddle.net/eVDe3/1/ Here's the corresponding code: 在此处检查此更新的提琴: http : //jsfiddle.net/eVDe3/1/这是相应的代码:

function reset(id) {
    $('#'+id+'Shad').animate({
        left: '+=100'
    },300);
}
$('#home').click(function() {
    reset($(this).attr('id'));
});

I see that you have set the "homeShad" id for the icon, but you used "shad" at points you have mentioned. 我看到您已经为图标设置了“ homeShad” ID,但是您在提到的地方使用了“ shad”。 I don't know you are aware of that or not, but ids and classes are case-sensitive, so you should write like below: 我不知道您是否意识到这一点,但是id和class区分大小写,因此您应该这样写:

$.reset = function()  {
$('#'+ $(this).attr("id")+" Shad").animate({
    left: '+=100'}, 300)
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM