javascript - Javascript toggle visibility showing text underneath
问题描述
When i click the *****
it shows the api key underneath on a new line. What I'm trying to do is when i click the stars it will hide the stars and show apikey then can someone point me where i went wrong.
My Image:
My Code:
<li class="list-group-item">
<i class="fa fa-tasks"></i> API Access:<span class="badge badge-danger pull-right"><span onclick="toggle_visibility('foo');">********</span>
<div id="foo" style="display:none;"><span class="label label-info"><?php echo ( isset($userInfo['apikey']) && !empty($userInfo['apikey']) ? $userInfo['apikey'] : "No" ); ?></span></div>
</span>
</a>
</li>
<script type="text/javascript">
<!--
function toggle_visibility(id) {
var e = document.getElementById(id);
if (e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
//-->
</script>`
解决方案
您可以通过多种方式改进您的代码,并使其按您的意愿工作。考虑到从 html 节点调用时的函数行为,我简化了 HTML 结构和 js。
<li class="list-group-item">
<i class="fa fa-tasks"></i>
API Access:
<span class="badge badge-danger pull-right">
<span onclick="show_apikey(<?php echo ( isset($userInfo['apikey']) && !empty($userInfo['apikey']) ? $userInfo['apikey'] : "No" ); ?>);">********</span>
</span>
</li>
<script type="text/javascript">
<!--
function show_apikey(apikey) {
this.innerText = apikey;
}
//-->
</script>
推荐阅读
- javascript - 比较不同的元素(如果我们有相同的基本字母)
- aws-appsync - 无突变的 AWS AppSync 订阅
- php - 如何从数组中删除特定数量的元素
- angular - 从 component.ts 在 Angular 7 中添加内联 css
- apache-nifi - 多个字段的 NiFi LookupRecord
- python - 你如何通过 Groupby 进行绘图?
- mysql - MariaDB SQL 插入与选择
- vue.js - 使用 UIKit uk-sortable 时留下一个副本
- javascript - 使用 SVG 时 ScrollIntoView 在 Firefox 中出现问题
- python - Celery + RabbitMQ - 使用 Celery 进行简单的消息传递