简体   繁体   English

想要在HTML页面内使用jquery或javascript显示和隐藏数据

[英]Want to show and hide data using jquery or javascript inside html page

I have two span s: 我有两个span

<span>Show link -3456
  <span>Hide link -n 1234 - 4567 -7777 -3456
  </span>
</span>

Basically first span 's link shows full number on click of show link and hides itself. 基本上,第一个span的链接在单击show链接时显示完整的数字,并隐藏自身。

On click of hide I want to again regain the original state with show link and last 4 digits of number.. 单击“隐藏”后,我想再次使用显示链接和最后4位数字来恢复原来的状态。

Could anyone please help me out I am stuck in such a simple code :( ? 任何人都可以帮助我,我陷入了这样一个简单的代码中:(?

You just simple need this have your html as 您只需要这个就可以将您的html作为

<table>
    <tr>
        <td>
<span class="show">Show link -3456</span>
<span class="hide" style="display:none">Hide link -n 1234 - 4567 -7777 -3456</span>
        </td>
    </tr>
<tr>
        <td>
<span class="show">Show link -3000</span>
<span class="hide" style="display:none">Hide link -n 1234 - 4567 -7777 -3456</span>
        </td>
    </tr>
</table>

And then using Jquery 然后使用Jquery

$("span.show").on("click",function(){

     $(this).hide();
     $("span.hide").show();

})

$("span.hide").on("click",function(){

     $(this).hide();
     $("span.show").show();

})

Updated For dynamically created multiples spans 更新了动态创建的倍数跨度

   $("table").on("click","span.show",function(){

     $(this).hide();
     $(this).siblings("span.hide").show();

})

$("table").on("click","span.hide",function(){

     $(this).hide();
     $(this).siblings("span.show").show();

})

Updated here is the fiddle working demo http://jsfiddle.net/cs6t3g48/2/ 这里更新的是小提琴工作演示http://jsfiddle.net/cs6t3g48/2/

You would start by linking to jQuery, then you would give the span that you want to hide an id. 您将从链接到jQuery开始,然后提供要隐藏ID的跨度。 It would happen like so: 它会像这样发生:

 $(function(){ $('#idofthespan').hide(); }); 

And if you want to respond to a click: 如果您想回应点击:

 $(function(){ $("#target").click(function() { $("#idofthespan").show(); }); }); 

 <span id="shortInfo">
    <a href="#" onclick="ShowInfo(true)">Show link</a> -3456
 </span>
 <span id="longInfo" style="display:none">
    <a href="#" onclick="ShowInfo(false)">Hide link</a>  -n 1234 - 4567 -7777 -3456
 </span>

Without JQuery: 没有JQuery:

 var ShowInfo = function (showLong) {
           if (showLong) {
               document.getElementById("shortInfo").style.display = "none";
               document.getElementById("longInfo").style.display = "block";
           }
           else
           {
               document.getElementById("shortInfo").style.display = "block";
               document.getElementById("longInfo").style.display = "none";
           }
       }

With JQuery: 使用JQuery:

 var ShowInfo = function (showLong) {
               if (showLong) {
                   $("#shortInfo").hide();
                   $("#longInfo").show();
               }
               else
               {
                   $("#shortInfo").show();
                   $("#longInfo").hide();
               }
           }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 AJAX、JQuery 和 HTML 显示/隐藏数据 - Show/Hide data using AJAX, JQuery and HTML 如果HTML数据为空,我想隐藏div,如果不是,我想显示。 HTML JavaScript - I want to hide div if html data is empty, I want to show if it is not. HTML JavaScript 如何使用JavaScript显示/隐藏隐藏的HTML表行(没有jQuery) - How to show/hide hidden HTML table rows using JavaScript (no jQuery) 如何使用Javascript / jQuery隐藏/显示启用/禁用HTML元素? - How hide/show enable/disable HTML elements using Javascript/jQuery? 想要使用“ this” jQuery显示和隐藏文本 - want to show and hide text using “this” jquery 如果我正在使用jQuery的.hide()和.show()方法,如何将javascript验证仅应用于HTML页面上的可见元素? - How to apply javascript validations only to visible elements on HTML page,if i am using jQuery's .hide() and .show() methods? 如果没有数据,如何隐藏表格并使用jquery或javascript显示表单 - how to hide the table if there is no data and show the form using jquery or javascript 在Jquery Mobile中如何显示/隐藏HTML页面 - In Jquery Mobile How to show / hide an HTML page 如何使用javascript jquery在html页面中显示json(array)响应 - how to show json (array)response in html page using javascript jquery 在PHP页面中使用JQuery显示/隐藏 - Show/Hide using JQuery in PHP page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM