01.
<!DOCTYPE html>
02.
<
html
>
03.
<
head
>
04.
<
meta
content
=
"width=device-width initial-scale=1.0 minimum-scale=1.0 maximum-scale=1.0 user-scalable=no"
name
=
"viewport"
>
05.
<
meta
charset
=
"UTF-8"
>
06.
<
link
rel
=
"stylesheet"
href
=
"https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css"
>
07.
08.
<
script
src
=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"
></
script
>
09.
<
script
>
10.
$.fn.extend({
11.
12.
add_button : function(option){
13.
14.
var input = $("<
input
type
=
'button'
>")
15.
.insertAfter($(this))
16.
;
17.
18.
if ( typeof option.val !== 'undefined' ) {
19.
input.val( option.val );
20.
}
21.
if ( typeof option.click !== 'undefined' ) {
22.
input.on("click", option.click );
23.
}
24.
if ( typeof option.id !== 'undefined' ) {
25.
input.prop("id", option.id);
26.
}
27.
if ( typeof option.class !== 'undefined' ) {
28.
input.addClass(option.class);
29.
}
30.
if ( typeof option.css !== 'undefined' ) {
31.
input.css( option.css );
32.
}
33.
return $(this);
34.
35.
}
36.
37.
});
38.
39.
$(function(){
40.
41.
$("#target").add_button({
42.
"val": "ボタン",
43.
"click" : function(){
44.
alert("クリックされました")
45.
},
46.
"id" : "btn"
47.
48.
});
49.
50.
});
51.
</
script
>
52.
53.
</
head
>
54.
<
body
>
55.
56.
<
span
id
=
"target"
>この後ろにボタン</
span
>
57.
58.
</
body
>
59.
</
html
>