質問

評価値が「未評価」の場合に非表示にしたいオブジェクト(この場合はjs-kitの評価オブジェクト)があります。これを行うための正しいjQueryセレクターの取得に問題があります。

以下のコードは動作するように見えますが、動作しません。

$(".js-rating-labelText:contains('unrated')").css("visibility", "hidden");  

.js-rating-labelText は、テキストに設定されるクラスです。

役に立ちましたか?

解決

  

テキストの内容に基づいて要素を選択する別の方法はありますか?

これを試してください:

$('.js-rating-labelText').filter(function(){
  return (/unrated/i).test($(this).text())
}).css('visibility', 'hidden');

他のヒント

独自のセレクターメソッドを作成できます

たとえば、次のことができるようにしたい場合:

$('.js-rating-label:hasText(unrated)');

hasText メソッドは次のように定義できます

$.expr[':']['hasText'] = function(node, index, props){
  return node.innerText.contains(props[3]);
}

props [3] には、「:hasText」の後の括弧内のテキストが含まれます。

おそらく、問題は関数の:contains( 'レーティングなし)部分にあります。テキストを任意のランダムな値に変更すると、同じ結果が生成されるため:

$("#ratingDiv:contains('somerandomtext')").hide();

@tgmdbmの優れた答えのわずかな変形。唯一の違いは、 hasText()引数と完全に一致する単一の子テキストノードを持つノードのみを選択することです。一方、 .innerText は、すべての子孫テキストノードの連結を返します。

  if( ! $.expr[':']['hasText'] ) {
     $.expr[':']['hasText'] = function( node, index, props ) {
       var retVal = false;
       // Verify single text child node with matching text
       if( node.nodeType == 1 && node.childNodes.length == 1 ) {
         var childNode = node.childNodes[0];
         retVal = childNode.nodeType == 3 && childNode.nodeValue === props[3];
       }
       return retVal;
     };
  }

js-kit関数がテキストを挿入した後ではなく、後にコードが実行されていることを確認します。

提供したHTMLに基づいて、テスト対象の「未評価の」テキストがどこから来ているのかわかりません。

ただし、それがそのdivのテキストの全体である場合は、直接テストするだけです。

if ($('.js-rating-labelText').text() == 'unrated'){
  $(this).hide();
}

hide()/ show()メソッドの使用を検討することもできます。

$(".js-rating-labelText:contains('unrated')").hide()

トピックを説明する最良の方法は、例と参照リンクを提供することです:-

例:- 次のjQueryセレクター構文は、すでに選択された(一致した)HTML要素のセットから最初またはn番目の要素を選択します。

$("selector:contains(searchText)")

Html:-

<table>
<tr><td>John Smith</td><td>Marketing</td></tr>
<tr><td>Jane Smith</td><td>Sales</td></tr>
<tr><td>Mike Jordon</td><td>Technology</td></tr>
<tr><td>Mary Jones</td><td>Customer Support</td></tr>
</table>
Search: <input id="txtSearch" type="text">
<input id="btnTestIt" type="button" value="Test It">
<input id="btnReset" type="reset" value="Reset">

Jquery:-

$(document).ready( function(){
    $("#btnTestIt").click( function(){
        var searchText = $("#txtSearch").val();
        $("td:contains('" + searchText + "')").css("background-color", "yellow");
    });
    $("#btnReset").click( function(){
        $("td").css("background-color", "white");
    });
});

javascript関数からのHTMLの一部を次に示します。

<div class="js-kit-rating" id="ratingDiv" style="width: 86px; visibility: hidden;" jk$initialized="true" path="/business/blogger-com" permalink="/businessblogger-com" freeze="yes" starcolor="Golden">
  <table class="js-ratings-tableWrapper" border="0" cellSpacing="0" cellPadding="0">
    <tbody>
      <tr>
        <td>
          <div class="js-ratingWrapper" style="width: 80px;">
          <div style="float: left; cssfloat: left;">
              <div style="width: 80px; height: 15px;">
<div class=" js-kit-objIcon" style="width: 16px; float: left; height: 15px; cssfloat: left;" imageURL="//js-kit.com/images/stars/golden.png">
  <img style="display: none;" src="//js-kit.com/images/stars/golden.png" complete="complete"/>
<div class=" js-kit-objIcon" style="width: 16px; float: left; height: 15px; cssfloat: left;" imageURL="//js-kit.com/images/stars/golden.png"/>
<div class=" js-kit-objIcon" style="width: 16px; float: left; height: 15px; cssfloat: left;" imageURL="//js-kit.com/images/stars/golden.png"/>
<div class=" js-kit-objIcon" style="width: 16px; float: left; height: 15px; cssfloat: left;" imageURL="//js-kit.com/images/stars/gray.png">
  <img style="display: none;" src="//js-kit.com/images/stars/gray.png" complete="complete"/>
<div class=" js-kit-objIcon" style="width: 16px; float: left; height: 15px; cssfloat: left;" imageURL="//js-kit.com/images/stars/gray.png"/>
<div class=" js-rating-labelText">
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top