English 中文(简体)
仅在输入字段中存在某些内容时才使用 javascript 指派类类
原标题:Assign class using javascript only if there is something in input field

当输入字段( 文本字段) 中存在某些值时, 是否有任何方法可以使用 javascript 来使用 javascript 来分配类( 文本字段) 。

详细 -- -- 详细 -- --

实际上在输入字段中使用了背景水标记。 所以当有人单击 & amp; 键入输入字段时, 我会用 cs 清除背景水标记图像( 使用: 焦点选择器 ) 。

这样,如果他移动到下一个字段,那么仍然可以使用打字文本显示背景水印(因为焦点是升到下一个字段)。

任何人都请?

如果是这样的话, 我将使用课目来清除背景水印图像!

问题回答
if($.trim($( input[type=text] ).val()).length) {
  $(some_target).addClass( new_class );
}

< a href=> "http://api.jquery.com/trim" rel="no follow".trim ()

.val ()

完整代码应显示如下:

$( input[type=text] ).on( keyup , function() {
  var targetInput = this; // keeping reference of input
  if($.trim(this.value).length) {
    // code for remove watermark
    $(targetInput).addClass( class_for_watermark );
  } else {
    // code for add watermark
    $(targetInput).removeClass( class_for_watermark );
  }
});

您不需要 :焦点 cs。

你是说这个?

if ( $input.val() ) {
  // Has a value, do something
  $element.addClass( myClass );
}




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签