[英]jQuery and CSS selectors
在過去,我使用以下代碼來獲取data-placeholder屬性中的文本,並將其用作div的占位符文本。
[contentEditable=true]:empty:not(:focus):before {
content:attr(data-placeholder)
}
這很好用,但在我目前的項目中我們需要做同樣的事情,除了它完全使用jQuery構建。 我知道jQuery有.empty()
.before()
.empty()
.before()
:not()
和.focus()
函數。 通過jQuery選擇器的工作方式,是否可以在jQuery選擇器中使用CSS選擇器?
var div = $([contentEditable=true]:empty:not(:focus):before);
如果沒有,那么在使用這么多功能時有更好的方法嗎?
簡單的解決方案是附加樣式標記:
var rule ='[contentEditable=true]:empty:not(:focus):before {'+
' content:attr(data-placeholder)'+
'}';
$('head').append('<style>'+rule +'</style>');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.