使用带有功能的.call()遍历元素

时间:2018-10-31 08:47:25

标签: javascript jquery

我想在加载时运行函数calcItemTotal,该函数执行简单的计算并更新元素的html。该函数使用$(this),所以我使用.call()来设置上下文。

该函数需要在加载时更新特定类的每个元素。

我的方法是获取该类的所有元素,然后根据有多少元素进行遍历,并调用calcItemTotal.call( Itotal[i] );。这将为每个元素$(this)设置正确的上下文。

对于单击的元素calcItemTotal.call( this );可以正常工作。

但是在.call( element[i] )设置下似乎不起作用,什么是正确的方法?

//update all item totals
$( window ).load(function() {

    //the totals
    var Itotal = document.querySelectorAll('.Itotal');

    //debugging = 12
    console.log('Itotal.length '+Itotal.length );

    //for each items total calculate and display total
    for (var i = 0; i < Itotal.length; ++i){

        //set what object called function (this)
        calcItemTotal.call( Itotal[i] );

        //debugging - this works correctly
        Itotal[i].style.color = 'red';

    };

    //update and display item total on click
    $(document).on('click' , '.item_add , .item_remove' , function() {  

        calcItemTotal.call( this ); //working

    });

});

编辑:添加了calcItemTotal

function calcItemTotal(){

    // strip all non numeric characters
    var selectedItem = 
    $(this).closest('.shopItem').find('.badge').text();

    //strip all non numeric characters except digits.
    var itemPrice = $(this).closest('.shopItem').find('.item_price').text();
    //convert integr to string > strip chars except digits and dots
    var priceStripped = itemPrice.toString().replace(/[^\d.-]/g, '');

    //item total
    var total = selectedItem * priceStripped;

    //rounded
    total = total.toFixed(2);

    //update element 
    $(this).closest('.shopItem').find('.Itotal').html('$'+total);

};  

1 个答案:

答案 0 :(得分:0)

当函数@Configuration public class ConfigurationApp { @Bean public DataSource dataSourceJdbc() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver"); dataSource.setUrl("jdbc:oracle:thin:@localhost:1521:orcl"); dataSource.setUsername("hossein"); dataSource.setPassword("myjava123"); dataSource.setDefaultAutoCommit(false); return dataSource; } @Bean public JdbcTemplate jdbcTemplate() { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSourceJdbc()); return jdbcTemplate; } @Bean public DAOImpl dao() { DAOImpl personDAO = new DAOImpl(); personDAO.setJdbcTemplate(jdbcTemplate()); return personDAO; } @Bean public PersonService personService() { PersonService personService = new PersonService(); personService.setPersonDAO(dao()); return personService; } } ////////////////////////////////////////// public class Person { private Integer id; private String name; private String family; private Integer password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFamily() { return family; } public void setFamily(String family) { this.family = family; } public Integer getPassword() { return password; } public void setPassword(Integer password) { this.password = password; } } ///////////////////////////////////////// import org.apache.commons.dbcp2.BasicDataSource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import java.util.List; @Repository public class DAOImpl { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public int add(Person person) { String sql = "insert into person(id,name,family,password) values(?,?,?,?)"; return this.jdbcTemplate.update(sql, person.getId(), person.getName(), person.getFamily(), person.getPassword()); } public void commit(){ BasicDataSource basicDataSource= (BasicDataSource) jdbcTemplate.getDataSource(); basicDataSource.setDefaultAutoCommit(true); } } /////////////////////////////////// import org.springframework.stereotype.Service; import java.util.List; @Service public class PersonService { private DAOImpl personDAO; public void setPersonDAO(DAOImpl personDAO){ this.personDAO=personDAO; } public void addPerson(Person person) { personDAO.add(person); this.personDAO.commit(); } } /////////////////////// public class MainApp { public static void main(String[] args) { Locale.setDefault(Locale.ENGLISH); AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext(ConfigurationApp.class); PersonService person=ac.getBean(PersonService.class); Person person1=new Person(); person1.setId(896); person1.setName("vali"); person1.setFamily("hassanpoor"); person1.setPassword(12579); person.addPerson(person1); } 需要jQuery calcItemTotal.call( $(this) );时。

您需要将其赋予您的功能。

$(this)

但是看起来 //set what object called function (this) calcItemTotal.call( $(Itotal[i]) ); 是普通的JS,也许可以处理普通的call()