使用jquery清除元素的属性

Clear a property of element with jquery

本文关键字:属性 元素 清除 jquery 使用      更新时间:2023-09-26

我正在设计一个基本表单。我有一个输入文本和一个按钮。当我点击按钮时,若"inputtext"里面并没有文本,"inputtext(输入文本)"里面就会出现一个"x"图标。

我把那个图标和jquery放在一起。但我不知道如何删除它。我想在用户点击输入文本时清除这个图标。这是我的jquery代码。

$('#lbl1').click(function () {
  if ($("#fill").val().length==0) {
        $("#fill").css({ background: "url(image/cikis.png) no-repeat right"});
  }
});
$('#fill').click(function () {
     //some codes here
});

这是我的html行:

 <label id="lbl1">Tıklama </label>
 <input type="text" id="fill" />
$("#fill").click(function() {
     $(this).css("background-image", "none");
});

您可以实现这样的

$('#fill').click(function () {
     $(this).css( "background-image", "none");
});

请参阅以下工作示例:

http://jsbin.com/UgIl/1/edit?html,js,输出

$(document).on("focus", "#fill", function (event) {
        $(this).filter(function () {
            return $(this).val() == ""
        }).css("background", "url(http://beoplay.com/resources/sbv-custom/img/linkbuttons/close-icon.png) right no-repeat transparent");
    });
    $(document).on("blur", "#fill", function (event) {
        $(this).filter(function () {
            return $(this).val() == "" 
        }).css("background-image", "none");
    });