通过JQuery在页面加载时触发单击JS代码

时间:2015-05-11 11:09:39

标签: javascript jquery

我在链接上运行以下代码: -

$('#expertprofiles-category-filters .filterby > a').on("click", function () {
    // Figure out which category to show.
    var categoryId = $(this).attr('rel');

    // Hide the other subfilters.
    $("div.category-panel[rel!=" + categoryId + "]").slideUp(100);

    // Show the subfilters of the category.
    $("div.category-panel[rel=" + categoryId + "]").delay(100).slideDown(400);
});

此代码运行正常,但问题是我也想在页面加载时运行此代码,以便自动选择任何一个链接。

我试过这个: -

$("#expertprofiles-category-filters .filterby").find('a').trigger('click');

但它不起作用......

感谢。

3 个答案:

答案 0 :(得分:1)

使用相同的选择器

$('#expertprofiles-category-filters .filterby > a').trigger('click');

答案 1 :(得分:1)

您也可以采取以下方式,

$('#expertprofiles-category-filters .filterby > a').on("click", function () {
    // Figure out which category to show.
    var categoryId = $(this).attr('rel');

    // Hide the other subfilters.
    $("div.category-panel[rel!=" + categoryId + "]").slideUp(100);

    // Show the subfilters of the category.
    $("div.category-panel[rel=" + categoryId + "]").delay(100).slideDown(400);
}).trigger("click"); // triggers click on DOM ready

答案 2 :(得分:0)

试试这个

05-11 13:55:28.763: E/AndroidRuntime(1924): FATAL EXCEPTION: main
05-11 13:55:28.763: E/AndroidRuntime(1924): java.lang.NullPointerException
05-11 13:55:28.763: E/AndroidRuntime(1924):     at android.os.Parcel.readException(Parcel.java:1333)
05-11 13:55:28.763: E/AndroidRuntime(1924):     at android.os.Parcel.readException(Parcel.java:1281)
05-11 13:55:28.763: E/AndroidRuntime(1924):     at com.tfl.extprotocolservice.ICallBackMessage$Stub$Proxy.onMessageReceived(ICallBackMessage.java:83)
05-11 13:55:28.763: E/AndroidRuntime(1924):     at com.tfl.extprotocolservice.ExtProtocolService$1.handleMessage(ExtProtocolService.java:127)
05-11 13:55:28.763: E/AndroidRuntime(1924):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 13:55:28.763: E/AndroidRuntime(1924):     at android.os.Looper.loop(Looper.java:137)
05-11 13:55:28.763: E/AndroidRuntime(1924):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-11 13:55:28.763: E/AndroidRuntime(1924):     at java.lang.reflect.Method.invokeNative(Native Method)
05-11 13:55:28.763: E/AndroidRuntime(1924):     at java.lang.reflect.Method.invoke(Method.java:511)
05-11 13:55:28.763: E/AndroidRuntime(1924):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-11 13:55:28.763: E/AndroidRuntime(1924):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-11 13:55:28.763: E/AndroidRuntime(1924):     at dalvik.system.NativeStart.main(Native Method)