jQuery first()不起作用

时间:2018-08-02 03:28:16

标签: javascript jquery

问题:我无法通过<li>选择first()标签的第一个孩子。我收到未定义的警报。抱歉,我是初学者。

这是我的HTML代码:

<li data-filtertext="" class="contentUpdate"><a href="#1-1" >Part 1</a></li>
<li data-filtertext="" class="contentUpdate"><a href="#1-2" >Part 2</a></li>

这是我的Jquery:

$(".contentUpdate").click(function() {
  alert($(this).first().attr('href'));
  $("#mainContent").html("new HTML");
});

2 个答案:

答案 0 :(得分:4)

尝试一下:

$(".contentUpdate").click(function() {
    alert($(this).children().first().attr('href'));
    $("#mainContent").html("new HTML");
});

答案 1 :(得分:0)

另一个:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/select_book_and_chapter" android:layout_width="393dp" android:layout_height="77dp" android:layout_alignParentTop="true" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:orientation="horizontal" android:padding="0dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <android.support.v7.widget.AppCompatSpinner android:id="@+id/books" android:layout_width="280dp" android:layout_height="55dp" android:layout_margin="10dp" android:layout_marginLeft="16dp" android:layout_marginStart="16dp" android:gravity="center" android:spinnerMode="dropdown" app:layout_constraintStart_toStartOf="parent" tools:layout_editor_absoluteY="320dp" /> <android.support.v7.widget.AppCompatSpinner android:id="@+id/chapters" android:layout_width="81dp" android:layout_height="54dp" android:layout_margin="10dp" android:layout_marginLeft="16dp" android:layout_marginStart="16dp" android:gravity="center" android:spinnerMode="dropdown" app:layout_constraintStart_toStartOf="parent" tools:layout_editor_absoluteY="320dp" /> </LinearLayout> <ScrollView android:id="@+id/scroll_verses" android:layout_width="387dp" android:layout_height="match_parent" android:layout_below="@+id/select_book_and_chapter" android:layout_above="@+id/controls" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:clipToPadding="false" android:fillViewport="true"> <LinearLayout android:id="@+id/select_verse" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" /> </ScrollView> <LinearLayout android:id="@+id/controls" android:layout_width="match_parent" android:layout_height="40dp" android:layout_alignParentBottom="true" android:orientation="horizontal" android:visibility="gone"> <android.support.v7.widget.AppCompatSpinner android:id="@+id/folders" style="@style/Widget.AppCompat.Spinner.Underlined" android:layout_width="299dp" android:layout_height="36dp" android:layout_marginBottom="8dp" android:layout_marginLeft="8dp" android:layout_marginStart="8dp" android:spinnerMode="dropdown" android:visibility="visible" /> <EditText android:id="@+id/create_new_name" android:layout_width="265dp" android:layout_height="36dp" android:layout_marginBottom="8dp" android:layout_marginLeft="8dp" android:layout_marginStart="8dp" android:ems="10" android:inputType="textPersonName" android:text="New Folder Nam" android:visibility="gone" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.578" app:layout_constraintStart_toEndOf="@+id/create_new_text" app:layout_constraintTop_toBottomOf="@+id/folders" /> <Button android:id="@+id/cancel_new_folder" android:layout_width="35dp" android:layout_height="37dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:visibility="gone" /> <Button android:id="@+id/save_scripture" android:layout_width="wrap_content" android:layout_height="37dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:onClick="saveToDB" android:text="Save" android:visibility="visible" /> </LinearLayout> </RelativeLayout> </FrameLayout>