我正在使用此代码在模型中旋转文本,但此代码在模型弹出窗口中不起作用?
var rotation = 0;
jQuery.fn.rotate = function (degrees) {
$(this).css({
'-webkit-transform': 'rotate(' + degrees + 'deg)',
'-moz-transform': 'rotate(' + degrees + 'deg)',
'-ms-transform': 'rotate(' + degrees + 'deg)',
'transform': 'rotate(' + degrees + 'deg)'
});
};
$('#btnRotateRight').click(function () {
rotation += 5;
$('.rotate').rotate(rotation);
});
$('#btnRotateLeft').click(function () {
rotation -= 5;
$('.rotate').rotate(rotation);
});
然后下面的代码是模型弹出。
$("#ViewDetail").append('<div class="modal-content"><div class="modal-header"><button id="btnRotateLeft" type="button">Rotate Left</button> <button id="btnRotateRight" type="button">Rotate Right</button><button type="button" class="close" data-dismiss="modal">×</button><h4 class="modal-title">Preview Cake</h4> </div> <div class="modal-body" style="text-align:center;"><div style="background-image:url(' + data.PrevRoundImg + ');max-width:320px;height:320px;background-repeat: no-repeat;padding-top:40%;" ><div class="rotate"><b id="msgtext" style="font-family:cursive;color:black; text-shadow: 0 0 3px White;font-size:20px;" >' + ckemsg + '</b></div></div></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div></div>');
最佳答案
您的脚本无法正常工作,因为您正在注册事件 $('#btnRotateRight').click( 和 $('#btnRotateLeft').click( on dynamic内容,因此对于这种情况,您需要在页面中呈现元素后注册事件或使用 Jquery on 事件,如下所示:
$(document).on("click", "#btnRotateRight", function () {
rotation += 5;
$('.rotate').rotate(rotation);
});
$(document).on("click", "#btnRotateLeft", function () {
rotation -= 5;
$('.rotate').rotate(rotation);
});
或者在渲染页面中的元素后注册事件,如下所示:
$("#btnCheck").click(function () {
$("#ViewDetail").append('<div class="modal-content"><div class="modal-header"><button id="btnRotateLeft" type="button">Rotate Left</button> <button id="btnRotateRight" type="button">Rotate Right</button><button type="button" class="close" data-dismiss="modal">×</button><h4 class="modal-title">Preview Cake</h4> </div> <div class="modal-body" style="text-align:center;"><div style="background-image:url(' + data.PrevRoundImg + ');max-width:320px;height:320px;background-repeat: no-repeat;padding-top:40%;" ><div class="rotate"><b id="msgtext" style="font-family:cursive;color:black; text-shadow: 0 0 3px White;font-size:20px;" >' + ckemsg + '</b></div></div></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div></div>');
RegisterEvents();
});
var rotation = 0;
function RegisterEvents() {
jQuery.fn.rotate = function (degrees) {
$(this).css({
'-webkit-transform': 'rotate(' + degrees + 'deg)',
'-moz-transform': 'rotate(' + degrees + 'deg)',
'-ms-transform': 'rotate(' + degrees + 'deg)',
'transform': 'rotate(' + degrees + 'deg)'
});
};
$('#btnRotateRight').click(function () {
rotation += 5;
$('.rotate').rotate(rotation);
});
$('#btnRotateLeft').click(function () {
rotation -= 5;
$('.rotate').rotate(rotation);
});
}
关于javascript - 在引导模式弹出窗口中旋转文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37521327/