设置动态创建按钮的位置

时间:2017-07-18 15:15:02

标签: jquery

我有这个代码动态创建一个按钮:

来自 @Rory McCrossan 的回答here

function createRefreshButton() {
    return $('<button/>', {
        text: 'Refresh Data',
        id: 'btn_refresh',
        click: ClickRefresh
    })
}
$('body').append(createRefreshButton())

function ClickRefresh() {
    alert('refresh');
}

创建按钮时,如何设置CSS属性,如:{position : absolute, top: 50, left : 50}

我尝试过这样使用.css()

function createRefreshButton() {
    return $('<button/>', {
        text: 'Refresh Data',
        id: 'btn_refresh',
        click: ClickRefresh
    }).css({position:absolute,top:50,left:50});
}
$('body').append(createRefreshButton())

function ClickRefresh() {
    alert('refresh');
}

没有成功!

2 个答案:

答案 0 :(得分:3)

您可以向用于配置其他属性的对象添加另一个属性 - 名为css

&#13;
&#13;
function createRefreshButton() {
    return $('<button/>', {
        text: 'Refresh Data',
        id: 'btn_refresh',
        css:{position:"absolute",top:"50px",left:"50px"}, // <-- right here
        click: ClickRefresh
    })
}
$('body').append(createRefreshButton())

function ClickRefresh() {
    alert('refresh');
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

&#13;
&#13;
   @Configuration
    @EnableAutoConfiguration
    public class SessionFactoryConfiguration {
        @Bean
        public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
            HibernateJpaSessionFactoryBean fact = new HibernateJpaSessionFactoryBean();
            fact.setEntityManagerFactory(emf);
            return fact;
        }
    }
&#13;
function createRefreshButton(_top,_left,index) {
    return $('<button/>', {
        text: 'Refresh Data'+index,
        id: 'btn_refresh'+index,
        click: ClickRefresh,
         css:{position:"absolute",top:(_top+"px"),left:(_left+"px")},
    })
}
for(var i=0;i<250;i=i+25){
$('body').append(createRefreshButton(i,i,i))
}
function ClickRefresh() {
    alert('refresh');
}
&#13;
&#13;
&#13;