获取Json数据并在拆分窗口onclick html上显示

斯里维迪亚

我有以下html代码在左窗格中显示父子列表。我想在onc​​lick期间格式化与每个项目相对应的json数据,并在右窗格中显示相同的数据。

但是,即使我单击子级,它也仅显示父级的数据。

以下是html代码:

屏幕截图

function getDetails(dom) {
  var jsonDataInString = dom.getAttribute("data-json"),
    jsonData = JSON.parse(jsonDataInString),
    ServiceElement = document.getElementById("Service"),
    PortElement = document.getElementById("Port"),
    NumberOfProcessElement = document.getElementById("NumberOfProcess"),
    HostsElement = document.getElementById("Hosts");

  if (jsonData) {
    ServiceElement.innerText = jsonData.Service;
    PortElement.innerText = jsonData.Port;
    NumberOfProcessElement.innerText = jsonData.NumberOfProcess;
    Hosts.innerText = jsonData.Hosts;
  }
}
.tree li {
  margin: 0px 0;
  list-style-type: none;
  position: relative;
  padding: 20px 5px 0px 5px;
}

.tree li::before {
  content: '';
  position: absolute;
  top: 0;
  width: 1px;
  height: 100%;
  right: auto;
  left: -20px;
  border-left: 1px solid #ccc;
  bottom: 50px;
}

.tree li::after {
  content: '';
  position: absolute;
  top: 30px;
  width: 25px;
  height: 20px;
  right: auto;
  left: -20px;
  border-top: 1px solid #ccc;
}

.tree>ul>li::before,
.tree>ul>li::after {
  border: 0;
}

.tree li:last-child::before {
  height: 30px;
}

.tree li r {
  border: 1px solid #080808;
  padding: 5px 10px;
  text-decoration: none;
  color: #666;
  font-family: arial, verdana, tahoma;
  font-size: 11px;
  display: inline-block;
  word-wrap: break-word;
  background-color: Tomato;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
}

.tree li g {
  border: 1px solid #080808;
  padding: 5px 10px;
  text-decoration: none;
  color: #666;
  font-family: arial, verdana, tahoma;
  font-size: 11px;
  display: inline-block;
  word-wrap: break-word;
  background-color: LightGreen;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
}

.split {
  height: 100%;
  width: 50%;
  position: fixed;
  z-index: 1;
  top: 0;
  overflow-x: hidden;
  padding-top: 20px;
}

.left {
  left: 0;
  background-color: white;
}

.right {
  right: 0;
  background-color: #ccc;
  word-wrap: break-word;
}
<div class="split left">
  <div class="tree" id="tree">
    <ul>
      <li onclick="getDetails(this)" data-json='{"Service":"ParentService","Hosts":"[\"parent.dev.com\"]","NumberOfProcess":"5","Port":"15080"}'>
        <g href="#">ParentService</g>
        <ul>
          <li onclick="getDetails(this)" data-json='{"Service":"ChildService","Hosts":"[\"child1.dev.com\"]","Port":"14758"}'>
            <g href="#">child1</g>
          </li>

          <li onclick="getDetails(this)" data-json='{"Service":"ChildService","Hosts":"[\"child2.dev.com\"]"}'>
            <r href="#">child2</r>
          </li>

          <li onclick="getDetails(this)" data-json='{"Service":"ChildService","Hosts":"[\"child3.dev.com\"]"}'>
            <r href="#">child3</r>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</div>


<div class="split right">
  <label for="Service"><b>Service:</b> </label>
  <span id="Service"></span>
  <br>
  <label for="Port"><b>Port:</b> </label>
  <span id="Port"></span>
  <br>
  <label for="NumberOfProcess"><b>Number Of Process:</b> </label>
  <span id="NumberOfProcess"></span>
  <br>
  <label for="Hosts"><b>Hosts:</b> </label>
  <span id="Hosts"></span>
</div>

mplungjan

您需要停止传播。每当您点击一个孩子时,您的点击就会传播到父母

这是一个不显眼的版本

function getDetails(e) {
  e.stopPropagation(); // cancel the event bubble
  var jsonDataInString = this.getAttribute("data-json"),
    jsonData = JSON.parse(jsonDataInString),
    ServiceElement = document.getElementById("Service"),
    PortElement = document.getElementById("Port"),
    NumberOfProcessElement = document.getElementById("NumberOfProcess"),
    HostsElement = document.getElementById("Hosts");
  if (jsonData) {
    ServiceElement.innerText = jsonData.Service || "Not available";
    PortElement.innerText = jsonData.Port || "Not available";
    NumberOfProcessElement.innerText = jsonData.NumberOfProcess || "Not available";
    Hosts.innerText = jsonData.Hosts || "Not available";
  }
}
document.querySelectorAll("[data-json]").forEach(function(el) {
  el.addEventListener("click",getDetails,false);
});
.tree li {
  margin: 0px 0;
  list-style-type: none;
  position: relative;
  padding: 20px 5px 0px 5px;
}

.tree li::before {
  content: '';
  position: absolute;
  top: 0;
  width: 1px;
  height: 100%;
  right: auto;
  left: -20px;
  border-left: 1px solid #ccc;
  bottom: 50px;
}

.tree li::after {
  content: '';
  position: absolute;
  top: 30px;
  width: 25px;
  height: 20px;
  right: auto;
  left: -20px;
  border-top: 1px solid #ccc;
}

.tree>ul>li::before,
.tree>ul>li::after {
  border: 0;
}

.tree li:last-child::before {
  height: 30px;
}

.tree li r {
  border: 1px solid #080808;
  padding: 5px 10px;
  text-decoration: none;
  color: #666;
  font-family: arial, verdana, tahoma;
  font-size: 11px;
  display: inline-block;
  word-wrap: break-word;
  background-color: Tomato;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
}

.tree li g {
  border: 1px solid #080808;
  padding: 5px 10px;
  text-decoration: none;
  color: #666;
  font-family: arial, verdana, tahoma;
  font-size: 11px;
  display: inline-block;
  word-wrap: break-word;
  background-color: LightGreen;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
}

.split {
  height: 100%;
  width: 50%;
  position: fixed;
  z-index: 1;
  top: 0;
  overflow-x: hidden;
  padding-top: 20px;
}

.left {
  left: 0;
  background-color: white;
}

.right {
  right: 0;
  background-color: #ccc;
  word-wrap: break-word;
}
<div class="split left">
  <div class="tree" id="tree">
    <ul>
      <li data-json='{"Service":"ParentService","Hosts":"[\"parent.dev.com\"]","NumberOfProcess":"5","Port":"15080"}'>
        <g href="#">ParentService</g>
        <ul>
          <li  data-json='{"Service":"ChildService","Hosts":"[\"child1.dev.com\"]","Port":"14758"}'>
            <g href="#">child1</g>
          </li>

          <li  data-json='{"Service":"ChildService","Hosts":"[\"child2.dev.com\"]"}'>
            <r href="#">child2</r>
          </li>

          <li data-json='{"Service":"ChildService","Hosts":"[\"child3.dev.com\"]"}'>
            <r href="#">child3</r>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</div>


<div class="split right">
  <label for="Service"><b>Service:</b> </label>
  <span id="Service"></span>
  <br>
  <label for="Port"><b>Port:</b> </label>
  <span id="Port"></span>
  <br>
  <label for="NumberOfProcess"><b>Number Of Process:</b> </label>
  <span id="NumberOfProcess"></span>
  <br>
  <label for="Hosts"><b>Hosts:</b> </label>
  <span id="Hosts"></span>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从mongoDB获取数据并在HTML上显示

jQuery获取数据并在HTML上显示

无法从php获取价值并在html上显示

使用Thymeleaf在HTML上显示数据

If Else 语法在 HTML 上显示数据

在HTML文件上显示Python数据

如何获取获取的数据以显示在 HTML 上

如何在html中的小屏幕上显示图像并在大屏幕上显示视频

HTML视频仅显示在窗口调整大小上

如何从@model获取数据并在angular上显示?

如何从android中的json获取数据并在RecyclerView中显示?

从JSON文件获取标记并在GoogleMap上显示

获取所有 Firebase 子项和值并在 HTML 上显示

使用 vanilla Javascript 获取数据并在 html 中显示

在Android中的ListView上显示CSS样式的HTML数据

将数据从Firebase显示到HTML网页上

角度:无法让我的数据显示在我的 HTML 文件上

使用香草JS在html表格上显示JSON

在Ionic App上显示JSON对象中的html元素

用jquery解析json并在html表中显示数据

AJAX通过php从sql获取数据,然后将数据值显示到html元素上

使用Volley for Network从JSON解析显示数据并在自动完成文本上显示数据

Angular 2 ngClass:获取插值({{}}),尝试在html上显示json时需要表达式

在网页[Django] [Python]上显示使用json2html生成的html吗?

如何使用HTML中的选择框在数据库中搜索特定用户并在表上显示该信息?

在Twig上显示JSON数据

如何读取带有印地语字符的CSV文件并在html上显示?

连接富文本字段(HTML)并在Access表单上显示结果

django-如何检查对象的选择并在html上显示相应的项目