.pin:after{
content: attr(content-after); // this will be set from jquery
}
$("#addPin").on("click", function(){
var elem = $("<div/>");
var id =$("#pinid").val(); // get text
elem.attr("id",id);
elem.addClass("pin");
elem.attr('content-after',id); // set content-after id which css will catch from that name
$("#carea").append(elem);
pins.push(new draggable(elem, updatePins));
});
、CSS Content
が動作する、thisを試して、た.pinのdivの中にそのラベルを追加し、私はあなたのコードを変更した、そしてそれは魅力
HTML
のように動作します
<div id="carea" class="carea">
<div id="pinsid" class="pinsid">
</div>
</div>
<div id="dsip"></div>
<input type="text" required="true" placeholder="Pin ID..." name="pinid" id="pinid">
<input type="button" value="Add Pin" id="addPin" >
CSS
#carea {
background: #19d223;
position: relative;
width: 200px;
height: 200px;
}
.pin {
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 100px;
background-image:url('https://i.downloadatoz.com/download/icon2/5/f/9/ca21cf4a9f7385a7d55b2efcf2b8c0d3.jpg');
cursor: move;
}
.pin label{
position: absolute;
left: 50%;
transform: translateX(-50%);
}
JS
(function ($) {
var draggable = function(element, updateFunc){
this.position = {x : 0, y : 0};
this.element = element;
var _this = this;
$(_this.element).draggable({
stop: function() {
_this.position.x = _this.element.position().left;
_this.position.y = _this.element.position().top;
updateFunc();
}
});
}
window.draggable = draggable;
}(window.jQuery));
var pins = [];
$("#addPin").on("click", function(){
var elem = $("<div/>").addClass("pin");
var label = document.createElement("label");
var detailt = document.createTextNode($('input#pinid').val());
label.appendChild(detailt);
elem.append(label);
$("#carea").append(elem);
pins.push(new draggable(elem, updatePins));
});
function updatePins(){
$("#pinslist ul").empty();
for(var p in pins){
var pin = pins[p];
}
}
//var dd = new draggable(null);
ここで
へ
fiddleです