1 回答

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果您調(diào)用onload除此之外的任何內(nèi)容都window不會產(chǎn)生任何效果。
如果您想在運(yùn)行函數(shù)之前檢查#createNewPostModaldiv,您可以執(zhí)行如下示例所示的操作:
$(document).ready(checkModal);
function checkModal () {
if($('#createNewPostModal').is(':visible')){ //if the container is visible on the page
document.getElementById('depACheckBoxLabel').innerHTML = "this";
document.getElementById('depBCheckBoxLabel').innerHTML = "works";
document.getElementById('depCCheckBoxLabel').innerHTML = "now";
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="modal fade" id="createNewPostModal">
<p>Select audience to share</p>
<div>
<input type="checkbox" id="depACheckBox">
<label id="depACheckBoxLabel" for="depACheckBox"></label>
<input type="checkbox" id="depBCheckBox">
<label id="depBCheckBoxLabel" for="depBCheckBox"></label>
<input type="checkbox" id="depCCheckBox">
<label id="depCCheckBoxLabel" for="CheckBox"></label>
</div>
</div>
在引用該容器時(shí),請隨意調(diào)整檢查:visible以適合您的需要。
此外,根據(jù)您的評論中的要求,如果您想調(diào)用此函數(shù),onclick可以執(zhí)行以下操作:
$('button').click(checkModal);
function checkModal () {
if($('#createNewPostModal').is(':visible')){ //if the container is visible on the page
document.getElementById('depACheckBoxLabel').innerHTML = "this";
document.getElementById('depBCheckBoxLabel').innerHTML = "works";
document.getElementById('depCCheckBoxLabel').innerHTML = "now";
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="modal fade" id="createNewPostModal">
<p>Select audience to share</p>
<div>
<input type="checkbox" id="depACheckBox">
<label id="depACheckBoxLabel" for="depACheckBox"></label>
<input type="checkbox" id="depBCheckBox">
<label id="depBCheckBoxLabel" for="depBCheckBox"></label>
<input type="checkbox" id="depCCheckBox">
<label id="depCCheckBoxLabel" for="CheckBox"></label>
</div>
</div>
<button onclick="checkModal()">Click</button>
只需交換button
您想要觸發(fā)該功能的任何元素即可。
最后,如果不需要等待#createNewPostModal
div 加載,那么只需像這樣調(diào)用你的函數(shù),它應(yīng)該可以工作:
$(document).ready(function() { document.getElementById('depACheckBoxLabel').innerHTML = window.user.depA.name; document.getElementById('depBCheckBoxLabel').innerHTML = window.user.depB.name; document.getElementById('depCCheckBoxLabel').innerHTML = window.user.depC.name; });
添加回答
舉報(bào)