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

jquery - Setting element data-icon with JavaScript - Stack Overflow

programmeradmin3浏览0评论

I have the following button element in my HTML

<button id="play-pause" aria-hidden="true"></button>

On the jQuery ready event, I run the following code:

$('#play-pause').attr('data-icon', '&#xe00a;')

That turns the HTML element into this

<button id="play-pause" aria-hidden="true" data-icon="&#xe00a;"></button>

Which renders like this in the browser (Chrome stable):

However, if I delete the JavaScript code, manually change the HTML to the following (it's the same thing as the JS waas doing) and refresh the page:

<button id="play-pause" aria-hidden="true" data-icon="&#xe00a;"></button>

then it renders like this:

What's the difference?

I suspect it's irrelevant but here's the CSS:

/* Use the following CSS code if you want to use data attributes for inserting your icons */
[data-icon]:before {
    font-family: '45sound';
    content: attr(data-icon);
    speak: none;
    font-weight: normal;
    font-variant: normal;
    text-transform: none;
    line-height: 1;
    font-style:normal;
}

I have the following button element in my HTML

<button id="play-pause" aria-hidden="true"></button>

On the jQuery ready event, I run the following code:

$('#play-pause').attr('data-icon', '&#xe00a;')

That turns the HTML element into this

<button id="play-pause" aria-hidden="true" data-icon="&#xe00a;"></button>

Which renders like this in the browser (Chrome stable):

However, if I delete the JavaScript code, manually change the HTML to the following (it's the same thing as the JS waas doing) and refresh the page:

<button id="play-pause" aria-hidden="true" data-icon="&#xe00a;"></button>

then it renders like this:

What's the difference?

I suspect it's irrelevant but here's the CSS:

/* Use the following CSS code if you want to use data attributes for inserting your icons */
[data-icon]:before {
    font-family: '45sound';
    content: attr(data-icon);
    speak: none;
    font-weight: normal;
    font-variant: normal;
    text-transform: none;
    line-height: 1;
    font-style:normal;
}
Share Improve this question asked May 21, 2013 at 23:05 David TuiteDavid Tuite 22.7k25 gold badges115 silver badges179 bronze badges 1
  • are you using jquery UI buttons? – km6zla Commented May 21, 2013 at 23:12
Add a ment  | 

1 Answer 1

Reset to default 7

Use Unicode value:

$('#play-pause').attr('data-icon', '\uE00A');
发布评论

评论列表(0)

  1. 暂无评论