根据标题将函数应用于单个jQuery元素

jez*_*pin 1 html jquery dom

我目前正在努力使用jQuery函数,该函数将文本框的标题添加为默认文本,直到用户点击它为止.我只想要一个特定的文本框来应用这个功能,所以我用'title ="搜索"'来修改它,以确保它只在标题搜索的字段上实现.但是,这似乎打破了功能.有谁知道下面的功能有什么问题?

$('input[type="text" title="Search"]').each(function() {
    this.value = $(this).attr('title');
    $(this).addClass('text-label');

    $(this).focus(function() {
        if (this.value == $(this).attr('title')) {
            this.value = '';
            $(this).removeClass('text-label');
        }
    });

    $(this).blur(function() {
        if (this.value == '') {
            this.value = $(this).attr('title');
            $(this).addClass('text-label');
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

Abh*_*ash 6

请尝试使用$('input[type="text"][title="Search"]').

在jQuery 中使用属性选择器时,需要单独编写每个单独的属性.

更新:

要使这适用于某个div中的元素,请尝试:

$('#id_of_the_div input[type="text"][title="Search"]')