使用 jQuery 将 id 添加到元素
Add id to element with jQuery
我有一个 HTML button
,我想向该元素添加一个 id
。但是我无法手动完成,所以我正在通过 jQuery.
寻找一个选项
<div class="mejs-button mejs-playpause-button mejs-play">
<button type="button" aria-controls="mep_0" title="Play" aria-label="Play" tabindex="0"></button>
</div>
您可以使用 prop()
或 attr()
方法与 jQuery 一起完成:
jQuery(document).ready(function($) {
$('.mejs-button.mejs-playpause-button.mejs-play button').prop('id', 'my-id');
//Or
$('.mejs-button.mejs-playpause-button.mejs-play button').attr('id', 'my-id');
});
$('.mejs-button button').prop('id', 'my-id');
console.log( $('.mejs-button button').prop('id') );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mejs-button mejs-playpause-button mejs-play"><button type="button"
aria-controls="mep_0" title="Play" aria-label="Play" tabindex="0"></button>
</div>
我有一个 HTML button
,我想向该元素添加一个 id
。但是我无法手动完成,所以我正在通过 jQuery.
<div class="mejs-button mejs-playpause-button mejs-play">
<button type="button" aria-controls="mep_0" title="Play" aria-label="Play" tabindex="0"></button>
</div>
您可以使用 prop()
或 attr()
方法与 jQuery 一起完成:
jQuery(document).ready(function($) {
$('.mejs-button.mejs-playpause-button.mejs-play button').prop('id', 'my-id');
//Or
$('.mejs-button.mejs-playpause-button.mejs-play button').attr('id', 'my-id');
});
$('.mejs-button button').prop('id', 'my-id');
console.log( $('.mejs-button button').prop('id') );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mejs-button mejs-playpause-button mejs-play"><button type="button"
aria-controls="mep_0" title="Play" aria-label="Play" tabindex="0"></button>
</div>