js forEach()对每个元素执行操作

时间:2015-10-08 14:00:17

标签: javascript

我有下列内容:

HB.formButtons.deactivatingButton = function(inputs) {
    inputs.forEach(function(argument){
        argument.parent().removeClass(HB.formButtons.SUBMIT_BUTTON_STYLE);
    });
}

我称之为各种各样的部分:

HB.formButtons.deactivatingButton($(HB.personalDetails.SUBMIT_PERSONAL_DETAILS_SELECTOR), $(HB.personalDetails.CANCEL_PERSONAL_DETAILS_SELECTOR));

控制台。抛出以下错误:

未捕获的TypeError:formsInput.forEach不是函数

为什么?

2 个答案:

答案 0 :(得分:1)

考虑到inputHTMLCollectionforEach无法处理集合。 forEach适用于Array

然而,您可以使其像

一样工作
HB.formButtons.deactivatingButton = function(inputs) {
    [].forEach.call(inputs, function(argument){
        argument.parent().removeClass(HB.formButtons.SUBMIT_BUTTON_STYLE);
    });
}

答案 1 :(得分:1)

如果你想传递两个元素的函数数组,你必须使用方括号register_post_type('accident', array( 'labels' => array( 'name' => _x( 'Accidents', 'post type general name', 'twentyfifteen-child' ), 'singular_name' => _x( 'Accident', 'post type singular name', 'twentyfifteen-child' ), 'menu_name' => _x( 'Accidents', 'admin menu', 'twentyfifteen-child' ), 'name_admin_bar' => _x( 'Accident', 'add new on admin bar', 'twentyfifteen-child' ), 'add_new' => _x( 'Add New', 'accident', 'twentyfifteen-child' ), 'add_new_item' => __( 'Add New Accident', 'twentyfifteen-child' ), 'new_item' => __( 'New Accident', 'twentyfifteen-child' ), 'edit_item' => __( 'Edit Accident', 'twentyfifteen-child' ), 'view_item' => __( 'View Accident', 'twentyfifteen-child' ), 'all_items' => __( 'All Accidents', 'twentyfifteen-child' ), 'search_items' => __( 'Search Accidents', 'twentyfifteen-child' ), 'parent_item_colon' => __( 'Parent Accidents:', 'twentyfifteen-child' ), 'not_found' => __( 'No accidents found.', 'twentyfifteen-child' ), 'not_found_in_trash' => __( 'No accidents found in Trash.', 'twentyfifteen-child' ) ), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array('slug' => 'accidents'), 'capability_type' => 'accident', 'capabilities' => array( 'publish_posts' => 'publish_accidents', 'edit_posts' => 'edit_accidents', 'edit_others_posts' => 'edit_others_accidents', 'delete_posts' => 'delete_accidents', 'delete_others_posts' => 'delete_others_accidents', 'read_private_posts' => 'read_private_accidents', 'edit_post' => 'edit_accident', 'delete_post' => 'delete_accident', 'read_post' => 'read_accident' ), 'map_meta_cap' => true, 'has_archive' => false, 'hierarchical' => false, 'supports' => array('title', 'editor') ));

  model1<-ucm(Sales~Var1+Var2+Var3, data=Sales, etc)

替代方法您可以在函数中使用特殊变量[]。在https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

描述