最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add span inside a button when using append() - Stack Overflow

programmeradmin4浏览0评论

I use append() when creating buttons. How do I have a < span > within the append text?

Basically I want to do this

<button type="button" class="btn btn-default" id="test1" >
     <span class="glyphicon glyphicon-arrow-left"></span>
</button>

here

$( '<button />' , { class: 'btn btn-default', type: 'button', id: 'test1' })

I use append() when creating buttons. How do I have a < span > within the append text?

Basically I want to do this

<button type="button" class="btn btn-default" id="test1" >
     <span class="glyphicon glyphicon-arrow-left"></span>
</button>

here

$( '<button />' , { class: 'btn btn-default', type: 'button', id: 'test1' })
Share Improve this question edited Mar 26, 2015 at 3:56 Fergoso asked Mar 26, 2015 at 3:20 FergosoFergoso 1,5823 gold badges22 silver badges45 bronze badges 1
  • put class="glyphicon glyphicon-arrow-left" in button 'value' attribute – Sagar Naliyapara Commented Mar 26, 2015 at 3:22
Add a ment  | 

3 Answers 3

Reset to default 2

You can use the html attribute as well like this

$('<button />', {
  class: 'btn btn-default',
  type: 'button',
  id: 'test1',
  html: '<span class="glyphicon glyphicon-arrow-left"></span>'
});

Here is a reference

var b = $( '<button />' , { class: 'btn btn-default', type: 'button', id: 'test1', html: '<span class="glyphicon glyphicon-arrow-left"></span>' });
$('body').append(b);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

You may use html:

    $( '<button />' , { 'class': 'btn btn-default', type: 'button', id: 'test1',
         html: '<span class="glyphicon glyphicon-arrow-left"></span>' })

Notice: class is a reserved word. So use it inside the quote.

How about this :

$( '<button />' , { 
     class   : 'btn btn-default', 
     type    : 'button', 
     id      : 'test1', 
     html    : '<span class="glyphicon glyphicon-arrow-left"></span>' 
})
发布评论

评论列表(0)

  1. 暂无评论