简体   繁体   中英

jQuery and CSS selectors

In the past I used the following code to take the take the text in the data-placeholder attribute and use it as a placeholder text for my div.

[contentEditable=true]:empty:not(:focus):before {
    content:attr(data-placeholder)
}

This worked great, but in my current project we need to do the same thing except it's entirely built using jQuery. I know jQuery has .empty() , .before() , :not() , and .focus() functions. With the way the jQuery selectors work, is it possible to use the CSS selector in the jQuery selector like so?

var div = $([contentEditable=true]:empty:not(:focus):before);

If not, then is there a better way to do this when working with so many functions?

Simple solution is to append a style tag:

var rule ='[contentEditable=true]:empty:not(:focus):before {'+
   ' content:attr(data-placeholder)'+
'}';
$('head').append('<style>'+rule +'</style>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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