在div下查找特定的类来处理子元素

时间:2015-08-08 17:53:57

标签: javascript jquery html

请允许我先粘贴我希望处理的HTML。

<div class="top-box-part">

    <h3 class="video-link-on-search">
        <a href="#">My Title</a>
        <div style="display: none;">
            <div id="colorbox-inline-1449860223">
                <div class="jwplayer-video">...</div>
            </div>

        </div>
    </h3>

    <h3 class="search-title"><a href="#">My Another Title</a></h3>

</div>

<div class="top-box-part">

    <h3 class="video-link-on-search">
        <a href="#">My Title 2</a>
        <div style="display: none;">
            <div id="colorbox-inline-1449860223">

            </div>

        </div>
    </h3>

    <h3 class="search-title"><a href="#">My second another title</a></h3>

</div>

正如您所看到的,div中有两个<h3>。我希望做的是,在主div <h3>中展示一个top-box-part。如果第一个<h3 class='video-link-on-search'>中有jwplayer-video,则显示此内容,然后隐藏第二个<h3 class='search-title'>,否则,隐藏第一个h3 class='video-link-on-search'并显示第二个<h3 class='search-title'>

页面中有许多<div class='top-box-part'>,每个<h3>有两个<h3>。我们只有一个在每个中显示一个package com.gamingbrothers.pat.passwordencrypter; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import java.util.ArrayList; import java.util.HashMap; import java.util.List; //table structure public class Dbhelper extends SQLiteOpenHelper { public static final String DB_NAME = "LogInInfo.db"; public static final String TABLE_NAME = "LogInInfo_table"; public static final String COL_1 = "ID"; public static final String COL_2 = "USER_NAME"; public static final String COL_3 = "USER_PASSWORD"; public static final String COL_4 = "APPLICATION"; public static final String COL_5 = "CUSTOMER"; public Dbhelper(Context context) { super(context, DB_NAME, null, 1); } c.close(); return notes_array; } @Override public void onCreate (SQLiteDatabase db) { db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT," + "USER_NAME TEXT,USER_PASSWORD TEXT," + "APPLICATION TEXT,CUSTOMER TEXT)"); } //Query Database with item selected from drop down public Cursor getChoiceData() { SQLiteDatabase db = this.getWritableDatabase(); Cursor result = db.rawQuery("select * from LogInInfo_table where CUSTOMER = ?",null); return result; } //Gather Data public Cursor getAllData() { SQLiteDatabase db = this.getWritableDatabase(); Cursor result = db.rawQuery("select * from LogInInfo_table",null); return result; } //getting customers for drop down public List<String> getAllCustomers(){ List<String> customers = new ArrayList<String>(); // Select All Query String selectQuery = "SELECT * FROM " + TABLE_NAME; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (cursor.moveToPosition(0)) { do { customers.add(cursor.getString(4)); } while (cursor.moveToNext()); } // closing connection cursor.close(); db.close(); // returning customers return customers; } //getting apps for drop down public List<String> getAllApps(){ List<String> apps = new ArrayList<String>(); // Select All Query String selectQuery = "SELECT * FROM " + TABLE_NAME; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (cursor.moveToPosition(0)) { do { apps.add(cursor.getString(3)); } while (cursor.moveToNext()); } // closing connection cursor.close(); db.close(); // returning apps return apps; } //creating update data function public boolean updateData(String ID,String User_Name,String Password,String Application,String Customer){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(COL_1,ID); contentValues.put(COL_2,User_Name); contentValues.put(COL_3,Password); contentValues.put(COL_4, Application); contentValues.put(COL_5,Customer); db.update(TABLE_NAME,contentValues, "ID = ?",new String[] {ID}); return true; } //deleting data public Integer deleteData (String ID){ SQLiteDatabase db = this.getWritableDatabase(); return db.delete(TABLE_NAME, "ID = ?",new String[]{ID}); } }

2 个答案:

答案 0 :(得分:1)

选中find()

&#13;
&#13;
$('.top-box-part').each(function() {
  $(this).find('h3').hide();
  if ($(this).find('h3 .jwplayer-video').length) {
    $(this).find('h3:first').show();
  } else {
    $(this).find('h3:not(:first)').show();
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="top-box-part">

  <h3 class="video-link-on-search">
        <a href="#">My Title</a>
        <div style="display: none;">
            <div id="colorbox-inline-1449860223">
                <div class="jwplayer-video">...</div>
            </div>

        </div>
    </h3>

  <h3 class="search-title"><a href="#">My Another Title</a></h3>

</div>

<div class="top-box-part">

  <h3 class="video-link-on-search">
        <a href="#">My Title 2</a>
        <div style="display: none;">
            <div id="colorbox-inline-1449860223">

            </div>

        </div>
    </h3>

  <h3 class="search-title"><a href="#">My second another title</a></h3>

</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

适当的选择器将是:

每个top_box-part中的每个第一个h1:

var firsts = $('.top-box-part h3:nth-child(1)');

每个.top-box-part中的每秒h1:

var seconds = $('.top-box-part h3:nth-child(2)');

现在您只需检查:

$.each(first,function(i,o){
   var first_element_of_current_top_box = o;
   var second_element_of_current_top_box = seconds[i];
   // here you can compare anything and do anything with those elements (ex. `.hasClass` etc.)
});

请注意,这种方式需要第1和第2个H1!