从同一元素jquery中获取输入

从同一元素jquery中获取输入,jquery,input,fadein,siblings,Jquery,Input,Fadein,Siblings,我真的开始着手处理jQuery了,但是我一直被一个小问题所困扰 当我聚焦在同一个父对象中的文本输入时,我需要淡入一个输入类型按钮: <span class="newsFeedMaxAutoLoad"> <span title="This is number of news ite...."> auto display: </span> <input type="text" onfocus="setAutoNews

我真的开始着手处理jQuery了,但是我一直被一个小问题所困扰

当我聚焦在同一个父对象中的文本输入时,我需要淡入一个输入类型按钮:

<span class="newsFeedMaxAutoLoad">
    <span title="This is number of news ite....">
         auto display:
    </span>
    <input type="text" onfocus="setAutoNews(this);" value="10" maxlength="99" class="newsAutoInput">
    <input type="button" value="save" class="button green small newsAutoSave"><!-- this is hidden by default by its class 'newsAutoSave'-->
</span>
但作为回报,我不断得到同样的错误:

语法错误,无法识别的表达式:[对象]

有没有人能告诉我,当我专注于文本输入时,如何淡入输入按钮?我想我错过了一个基本点,但我无法确定

谢谢,
John

您正在尝试用字符串连接对象

objParent
是一个jQuery对象。您可以这样做:

objParent.find('.newsAutoSave').fadeIn()

另外,如果变量名是jQuery对象,则通常在变量名前面加上
$
。这使您更容易记住您使用的是jQuery对象,而不是字符串或常规DOM节点

您可以将代码更改为以下内容:

function setAutoNews(obj){
   var $objParent = $(obj).parent();
   $objParent.find('.newsAutoSave').fadeIn();
}

我可能会采取不同的方法,根本不创建函数:

$(document).ready( function() {
  $('.newsAutoInput').click(function() {
    $(this).next('.newsAutoSave').fadeIn();
  });
});
这避免了使用内联JS,这使得维护代码更加容易。因此,您的HTML将如下所示:

<span class="newsFeedMaxAutoLoad">
    <span title="This is number of news ite....">
         auto display:
    </span>
    <input type="text" value="10" maxlength="99" class="newsAutoInput">
    <input type="button" value="save" class="button green small newsAutoSave"><!-- this is hidden by default by its class 'newsAutoSave'-->
</span>

自动显示:

这里有一个演示:

Ahhh。。。下一个我不知道那件事。这样就抓住了父元素中的下一个元素。。。美好的非常感谢!:)我想这是最有意义的。从顶部启动事件侦听器,而不是像猴子一样上下遍历。非常感谢你!事实上,我一直在和我的一个朋友争论到底是内联事件侦听器更好,还是这种方法更好。对我来说,底线是,当一个事件监听器可以像您所做的那样添加到脚本文件中时,1000个内联onclick在客户端和服务器之间传输的数据要多得多。再次感谢:)
//find the elements we want to bind to for the focus event
$('.newsFeedMaxAutoLoad').children('.newsAutoInput').on('focus', function () {

    //select the next element which is the button,
    //we could also use `.siblings('.newsAutoSave')` instead of `.next()` if the button may not always be the exact next sibling element
    $(this).next().fadeIn(250);
});
<span class="newsFeedMaxAutoLoad">
    <span title="This is number of news ite....">
         auto display:
    </span>
    <input type="text" value="10" maxlength="99" class="newsAutoInput">
    <input type="button" value="save" class="button green small newsAutoSave"><!-- this is hidden by default by its class 'newsAutoSave'-->
</span>