检查是否有更有效的方法来处理这个问题 - jQuery 或 JS。按钮 ID 与三种类型的文档相匹配,这些文档将其内容插入到模式中的文本区域中。具有单个名称的 ID 工作正常,具有多个单词的名称需要正确显示。样式无关紧要——模态标题才是关键。插入内容只是与按钮 ID 同名的外部文档(即外部文档是primarybutton.html、primarybutton.js、primarybutton.css)
$(".codeBtn").click(function() {
var btnId = $(this).attr("id");
var htmlUrl = "./modals/" + btnId + ".html";
var jsUrl = "./modals/" + btnId + ".js";
var cssUrl = "./modals/" + btnId + ".css";
var id = this.id;
if(id == "primarybutton") {
$("#modalTitle").html("Primary Button Code");
} else if(id == "secondarybutton"){
$("#modalTitle").html("Secondary Button Code");
} else if(id == "tertiarybutton"){
$("#modalTitle").html("Tertiary Button Code");
} else if(id == "segmentedcontrols"){
$("#modalTitle").html("Segmented Controls Code");
} else if(id == "time-picker"){
$("#modalTitle").html("Time Picker Code");
} else if(id == "textfield"){
$("#modalTitle").html("Text Field Code");
} else if(id == "radiobuttons"){
$("#modalTitle").html("Radio Button Code");
} else if(id == "textlink"){
$("#modalTitle").html("Text Link Code");
} else if(id == "tabbtn"){
$("#modalTitle").html("Tabs Code");
}
else {
$("#modalTitle").html(btnId + " Code");
}
$.ajax({
url : htmlUrl,
dataType: "text",
success : function (data) {
$(".htmlCode").text(data);
}
});
$.ajax({
url : jsUrl,
dataType: "text",
success : function (data) {
$(".jsCode").text(data);
}
});
$.ajax({
url : cssUrl,
dataType: "text",
success : function (data) {
$(".cssCode").text(data);
}
});
});
<button data-target="#codeModal" data-toggle="modal" class="codeBtn" id="primarybutton">Get Code</button>
<div class="modal fade text-center" id="codeModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modalheader">
<h1 id="modalTitle">Navigation Code</h1>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="modal-content-code">
<label>HTML:</label>
<textarea class="js-copytextarea htmlCode" readonly="readonly" rows="5">Hello. This is textarea test bed #1</textarea>
<button class="js-textareacopybtn btn-primary">Copy HTML</button>
</div>
<div class="modal-content-code">
<label>CSS:</label>
<textarea class="js-copytextarea cssCode" readonly="readonly" rows="5">Hi! Welcome to textarea test bed #2 </textarea>
<button class="js-textareacopybtn btn-primary">Copy CSS</button>
</div>
<div class="modal-content-code">
<label>JS:</label>
<textarea class="js-copytextarea jsCode" readonly="readonly" rows="5">Hi! Welcome to textarea test bed #2 </textarea>
<button class="js-textareacopybtn btn-primary">Copy JS</button>
</div>
</div>
</div>
</div>
</div>
最佳答案
这个问题有几个很好的解决方案。
- 使用映射在按钮 ID 和模式文本之间进行映射
- 在包含模式文本的按钮上使用
data-*
属性。
解决方案1:
var btnModalMap = {
"primarybutton": "Primary Button",
"secondarybutton": "Secondary Button",
"tertiarybutton": "Tertiary Button",
"segmentedcontrols": "Segmented Controls",
"time-picker": "Time Picker",
//...
//...
}
$(".codeBtn").click(function() {
var btnId = $(this).attr("id");
var htmlUrl = "./modals/" + btnId + ".html";
var jsUrl = "./modals/" + btnId + ".js";
var cssUrl = "./modals/" + btnId + ".css";
var id = this.id;
var modalText = btnModalMap[id] || btnId; // default to btnId if id not found in map
$("#modalTitle").html(modalText + " Code")
// do ajax here
});
解决方案2:
HTML
<button class="codeBtn" id="primarybutton" data-modaltext="Primary Button">Primary</button>
<button class="codeBtn" id="secondarybutton" data-modaltext="Secondary Button">Secondary</button>
<button class="codeBtn" id="tertiarybutton" data-modaltext="Tertiary Button">Tertiary</button>
<button class="codeBtn" id="segmentedcontrols" data-modaltext="Segmented Controls">Segmented</button>
<button class="codeBtn" id="time-picker" data-modaltext="Time Picker">Time</button>
JS
$(".codeBtn").click(function() {
var btnId = $(this).attr("id");
var htmlUrl = "./modals/" + btnId + ".html";
var jsUrl = "./modals/" + btnId + ".js";
var cssUrl = "./modals/" + btnId + ".css";
var id = this.id;
var modalText = $(this).attr("data-modaltext") || btnId; // default to btnId in case there is no modal text defined
$("#modalTitle").html(modalText + " Code")
// do ajax here
});
关于javascript - jQuery 合并过多的 if/else 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54600330/