最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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');
Post a comment

comment list (0)

  1. No comments so far