javascript - Javascript : Using a Toggle to Show More or Less

标签 javascript jquery html

我需要一些帮助。我必须编写一个 javascript 代码来切换 a 元素以显示或多或少。如果我点击显示更多,它应该显示更多,并带有一个显示更少的链接。

我遇到的问题

1. 如果我点击显示更多关于作者,它不仅会显示更多关于作者的信息,还会显示有关图书描述以及这本书是谁的更多信息为..我不希望它这样做,我只是希望它向我显示有关该特定信息的更多信息。

2. 它也没有向我显示或给我less 链接选项。

这是我的 HTML 和 JavaScript 代码。

$(document).ready(function() {
    
    $("#jdom").click(function() {
        $("a").toggle(function () {
            $(this).text("").siblings("div").hide();
        }, function() {
            $(this).text("Less").siblings("div").show();
        });
    });
    
});
article, aside, figure, figcaption, footer, header, nav, section {
    display: block;
}
* {
	margin: 0;
	padding: 0;
}
body {
	font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 87.5%;
	width: 650px;
	margin: 0 auto;
	border: 3px solid blue;
}
section {
	padding: 15px 25px;
}
h1 { 
	font-size: 150%;
}
h2 {
	font-size: 120%;
	padding: .25em 0 .25em 25px;
}
div.hide {
	display: none;
}
ul {
	padding-left: 45px;
}
li {
	padding-bottom: .4em;
}
p, a {
	padding-bottom: .4em;
	padding-left: 25px;
}
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
    <title>Expand/Collapse</title>
	<link rel="shortcut icon" href="images/favicon.ico">
	<link rel="stylesheet" href="index.css">
	<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
	<script src="http://code.jquery.com/jquery-latest.min.js"></script>
   	<script src="subset_expansion.js"></script>   	
</head>

<body>
	<section id="jdom">
		<h1>Murach's JavaScript and DOM Scripting</h1>
		<h2>Book description</h2>
		<div>
            <p>You can read other JavaScript books from start to finish and still not
            know how to develop dynamic websites like you want to.</p>
        </div>
        <div class="hide">
            <p>But now, you can go from JavaScript beginner to DOM scripting expert in a 
		    single book! Fast-paced, professional, and packed with expert practices, our 
			new JavaScript book.</p>
		</div>
		<a href="#">Show more</a>			
		
		<h2>About the author</h2>
		<div>
            <p>Ray Harris is an expert JavaScript programmer. That will be obvious to you 
            as soon as you review the 20 complete applications that he presents in this 
            book.</p>
        </div>
        <div class="hide">
			<p>Ray Harris is much more than a JavaScript programmer.</p>
			<p>So when Ray said he wanted to write for us, it didn't take us long to hire 
			him.</p>
		</div>
		<a href="#">Show more</a>
			
		<h2>Who this book is for</h2>
		<div>
            <p>Due to our unique presentation methods and this book's modular organization,
			this is the right book for any web developer who wants to use JavaScript effectively.</p>
		</div>
		<div class="hide"> 
			<p>Here's just a partial list of who can use this book:</p>
			<ul>
				<li>Web developers who know HTML and CSS and are ready to master JavaScript.</li> 
				<li>Web developers who program in ASP.NET, JSP, or PHP on the server side and 
				now want to master client-side coding.</li>
				<li>Web developers who have already read 3 or 4 JavaScript or DOM scripting books 
				but still don't know how to do the type of DOM scripting that's required in 
				real-world applications</li>
			</ul>
		</div>
		<a href="#">Show more</a>				
	
	</section>
</body>
</html>

我不知道我做错了什么。

最佳答案

试试这个,

    $(document).ready(function() {
      $(document).on('click','a',function(e){
         $(this).text( ($(this).text()  == "Show more") ? "Less":"Show more" ) ;
         $(document).find($(this).attr('data-target')).toggle();

         /*OR WITH ANIMATION*/
         /*
         if($(this).text()  == "Show more"){
            $(this).text("Less");
            $(document).find($(this).attr('data-target')).slideDown(300);
         }else{
            $(this).text("Show more");
            $(document).find($(this).attr('data-target')).slideUp(300);
         }*/
      });



    /*MORE ACTION*//*
     $(document).on('click','#jdom',function(e){
             $(document).find('a').each(function(){
                     $(this).trigger('click');
             })
          });
    */
    });

HTML 部分更新如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Expand/Collapse</title>
    <link rel="shortcut icon" href="images/favicon.ico">
    <link rel="stylesheet" href="index.css">
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="subset_expansion.js"></script>     
</head>

<body>
    <section id="jdom">
        <h1>Murach's JavaScript and DOM Scripting</h1>
        <h2>Book description</h2>
        <div>
            <p>You can read other JavaScript books from start to finish and still not
            know how to develop dynamic websites like you want to.</p>
        </div>
        <div class="hide" id="bookDesc">
            <p>But now, you can go from JavaScript beginner to DOM scripting expert in a 
            single book! Fast-paced, professional, and packed with expert practices, our 
            new JavaScript book.</p>
        </div>
        <a href="#" data-target="#bookDesc">Show more</a>           

        <h2>About the author</h2>
        <div>
            <p>Ray Harris is an expert JavaScript programmer. That will be obvious to you 
            as soon as you review the 20 complete applications that he presents in this 
            book.</p>
        </div>
        <div class="hide" id="author">
            <p>Ray Harris is much more than a JavaScript programmer.</p>
            <p>So when Ray said he wanted to write for us, it didn't take us long to hire 
            him.</p>
        </div>
        <a href="#" data-target="#author">Show more</a>

        <h2>Who this book is for</h2>
        <div>
            <p>Due to our unique presentation methods and this book's modular organization,
            this is the right book for any web developer who wants to use JavaScript effectively.</p>
        </div>
        <div class="hide" id="bookDet"> 
            <p>Here's just a partial list of who can use this book:</p>
            <ul>
                <li>Web developers who know HTML and CSS and are ready to master JavaScript.</li> 
                <li>Web developers who program in ASP.NET, JSP, or PHP on the server side and 
                now want to master client-side coding.</li>
                <li>Web developers who have already read 3 or 4 JavaScript or DOM scripting books 
                but still don't know how to do the type of DOM scripting that's required in 
                real-world applications</li>
            </ul>
        </div>
        <a href="#" data-target="#bookDet">Show more</a>                

    </section>
</body>
</html>

关于javascript - Javascript : Using a Toggle to Show More or Less,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40648608/

相关文章:

javascript - GA GEO 图表城市标记不正确

javascript - jQuery Mobile 下一页不起作用

javascript - 为什么 onsubmit 不起作用,如果我在控制台中调用它运行的函数?

javascript - 防止文本选择退出 div

javascript - onchange 检查密码是否相似

javascript - 用于 php 生成的图像库的水平 slider

javascript - 打印时尝试将 div 带到最后一页的页脚

javascript - 在多部分/表单数据上传的各个部分设置内容长度 header

javascript - 使用 javascript 设置 Tab 键顺序

html - 如何在CSS中移动单选按钮