我试图在$ .confirm()上的onContentReady函数中追加一个孩子,但似乎对我不起作用。这是代码
HTML
<button onclick="btnclick()">click</button>
的Javascript
function btnclick() {
// body...
$.confirm({
title:'test',
content: '<div class=".confirm_body"> </div>',
onContentReady:function() {
var body = this.$content.find('.confirm_body');
var h3 = document.createElement('h3');
h3.appendChild(document.createTextNode("child"));
body.appendChild(h3);
}
});
}
例外
jquery.min.js:2 jQuery.Deferred exception: body.appendChild is not a function TypeError: body.appendChild is not a function
at Jconfirm.onContentReady (http://localhost/NMSCST_CLINIC_INFORMATION_SYSTEM/:154:8)
at Array.<anonymous> (http://localhost/NMSCST_CLINIC_INFORMATION_SYSTEM/assets/jquery-confirm-v3.3.0/jquery-confirm.min.js:10:6129)
at j (http://localhost/NMSCST_CLINIC_INFORMATION_SYSTEM/assets/vendor/jquery/jquery.min.js:2:29568)
at k (http://localhost/NMSCST_CLINIC_INFORMATION_SYSTEM/assets/vendor/jquery/jquery.min.js:2:29882) undefined
r.Deferred.exceptionHook @ jquery.min.js:2
我的代码没有显示孩子。
我该怎么办?请帮忙
答案 0 :(得分:1)
我猜你的选择器在else if (userGuess === computerSelection) { //Change from else if --> if
wins++;
alert('The force is strong with you!');
guessesLeft = 10;
guessesSoFar = [];
computerSelection = computerChoices[Math.floor(Math.random() * computerChoices.length)];
}
else if (guessesLeft === 0) {
losses++;
alert('Better luck next time!');
guessesLeft = 10;
guessesSoFar = [];
computerSelection = computerChoices[Math.floor(Math.random() * computerChoices.length)];
}
// transfers info into the html
var html =
"<p id='wins'>Intuitive<br>Victories:<br>" + wins + "</p>" +
"<p id='losses'>Incorrect<br>Guesses:<br>" + losses + "</p>" +
"<p id='attempts'>Remaining<br>Attempts:<br>" + guessesLeft + "</p>" +
"<p id='guesses'>Predictions:<br> " + guessesSoFar.join(", ") + "</p>";
document.querySelector('#game').innerHTML = html;
中不正确,如果this.$content.find('confirm_body')
是confirm_body
元素,则使用id
,例如,
#
答案 1 :(得分:1)
body
是一个jQuery对象,而不是DOM对象,所以你应该使用
body.append($(h3));
最好保持一致性,坚持使用所有jQuery方法,而不是混合原生DOM元素和jQuery对象。