javascript - 如何更改代码来控制某些按钮的名称?
问题描述
有一个代码
$("button").click(function() {
var btn = this;
if (btn.loaded) {
$(btn).prev("div").html(btn.originalContent);
btn.loaded = false;
$(btn).text('Get Dynamic chart');
} else {
btn.originalContent = $(btn).prev("div").html();
$(btn).prop('disabled', true);
$.get($(btn).data("url"), function(html) {
$(btn).prev("div").html(html);
$(btn).prop('disabled', false).text('Close');
btn.loaded = true;
});
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="div1">
<h2>Dynamic chart1</h2>
</div>
<button data-url="https://stackoverflow.getsandbox.com:443/dashboard?a=1">Get Dynamic chart1</button>
<div id="div2">
<h2>Dynamic chart2</h2>
</div>
<button data-url="https://stackoverflow.getsandbox.com:443/dashboard?a=2">Get Dynamic chart2</button>
<div id="div3">
<h2>Dynamic chart3</h2>
</div>
<button data-url="https://stackoverflow.getsandbox.com:443/dashboard?a=3">Get Dynamic chart3</button>
<div id="div4">
<h2>Dynamic chart4</h2>
</div>
<button data-url="https://stackoverflow.getsandbox.com:443/dashboard?a=4">Get Dynamic chart4</button>
<div id="div5">
<h2>Dynamic chart5</h2>
</div>
<button data-url="https://stackoverflow.getsandbox.com:443/dashboard?a=5">Another button</button>
所以,Button 5
每个人都有不同的名字,但是当你点击它的时候,脚本的名字变成了"Close"
,这是正常的。但是在点击关闭按钮后,按钮的名称被分配了一个脚本Get Dynamic chart
而不是"Another button"
,即不以原生名称为准。如何确保5th button
脚本没有为按下之前和按下之后分配名称,或者如何在脚本中为和分配其5th button
名称?谢谢opening
closing
returning to the original name
解决方案
但是有什么不清楚的呢?
第一次单击它时,它将运行:
btn.originalContent = $(btn).prev("div").html();
$(btn).prop('disabled', true);
$.get($(btn).data("url"), function(html) {
$(btn).prev("div").html(html);
$(btn).prop('disabled', false).text('Close');
btn.loaded = true;
});
}
所以btn.loaded=true
,text
现在是“关闭”
下次单击时,将运行:
$(btn).prev("div").html(btn.originalContent);
btn.loaded = false;
$(btn).text('Get Dynamic chart');
它完全按照你告诉它去做。
一个更好的问题是......你到底是什么意思?
推荐阅读
- python - 不能按照我想要的方式附加 2 个字符串
- soap - 使用 Nginx 反向代理限制 SOAP 调用
- excel - 您如何防止调整形状/文本框的大小但允许移动?
- sqlite - 为什么 PRAGMA schema.index_list(table-name); 不显示主键?
- python-3.x - Beautifulsoup 对数组中文本的抓取问题
- python - pygame.Rect.move_ip() 不更新 rect 属性
- java - 如何将 JavaFX GridPane 替换为另一个?我尝试并收到重复错误
- latex - 乳胶逐字:如何显示由树命令创建的文件目录?
- angular - 如何在 Angular 6 中处理页面上的多个下拉过滤器?
- javascript - 使用 lodash 显示嵌套 json 的所有值