Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在“隐藏/显示”按钮中显示额外文本_Javascript_Html - Fatal编程技术网

Javascript 在“隐藏/显示”按钮中显示额外文本

Javascript 在“隐藏/显示”按钮中显示额外文本,javascript,html,Javascript,Html,我的问题是,当我按下按钮时,段落文本应隐藏,并显示“单击查看更多”,然后再次按下同一按钮,段落文本应可见。我不确定是否回答了您的问题。 但是这里有一个使用vanilla JS的简单解决方案,通过设置段落的高度并将cssoverflow属性设置为hidden。然后通过编程将溢出属性设置为无,您可以按如下方式存档: <body> <p style="width: 500px; height: 20px; overflow: hidden;" id="m

我的问题是,当我按下按钮时,段落文本应隐藏,并显示“单击查看更多”,然后再次按下同一按钮,段落文本应可见。

我不确定是否回答了您的问题。 但是这里有一个使用vanilla JS的简单解决方案,通过设置段落的高度并将css
overflow
属性设置为
hidden
。然后通过编程将
溢出
属性设置为
,您可以按如下方式存档:

<body>
<p style="width: 500px; height: 20px; overflow: hidden;" id="myParagraph">
    Lorem ipsum, dolor sit amet consectetur adipisicing elit. Blanditiis amet sunt laudantium officia 
    sed porro dolorem odit iusto quidem enim ab nihil, quos quod ad quam atque veritatis iure architecto?

</p>
<button id="btnSeeMore">See More</button>
<script>    
    var seeMore = true;
    var myParagraph = document.getElementById('myParagraph');
    var myBtn = document.getElementById('btnSeeMore');
    
    myBtn.addEventListener('click',function(e){
        console.log("event",e);
        if(seeMore){
            
            console.log("if");
            myParagraph.style.overflow = "none"; 
            myParagraph.style.height = "100px"; 
            myBtn.innerHTML = "See Less";
            seeMore = false;
           
        }else{
            console.log("else");
            myParagraph.style.overflow = "hidden"; 
            myParagraph.style.height = "20px"; 
            myBtn.innerHTML = "See More";
            seeMore = true;
        }
        
    });
</script>

Lorem ipsum,dolor sit amet Concertetur Adipising Elite。艾美布兰迪酒店 你是一个真正的建筑师吗?

查看更多 var seeMore=真; var my段落=document.getElementById('my段落'); var myBtn=document.getElementById('btnSeeMore'); myBtn.addEventListener('click',函数(e){ console.log(“事件”,e); 如果(请参阅更多){ 控制台日志(“如果”); myparagration.style.overflow=“无”; myparagration.style.height=“100px”; myBtn.innerHTML=“查看较少”; seeMore=假; }否则{ 控制台日志(“其他”); myparagration.style.overflow=“隐藏”; myparagration.style.height=“20px”; myBtn.innerHTML=“查看更多”; seeMore=true; } });
这是JSFIDLE
我想这是您需要的代码

    <div id="myParagraph">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Esse nostrum 
    dolore sapiente ducimus facere accusamus illo libero, vero nihil impedit.</div>
<div>
    <button id="btnShowMore">Show More</button>
    <button id="btnHide">Hide</button>
</div>
<script>
    const myParagraph = document.getElementById("myParagraph");
    const btnShowMore = document.getElementById("btnShowMore");
    const btnHide = document.getElementById("btnHide");

    let myText = myParagraph.innerHTML;
    btnShowMore.addEventListener("click",()=>{
        // everytime when the user click show more
        myParagraph.innerHTML += myText;
    })
    btnHide.addEventListener("click",()=>{
        // if you want to display the first text use myText if you don't want to siplay anything use ""
        myParagraph.innerHTML = myText;
    })
</script>
Lorem ipsum door sit amet,concetetur adipsicing elit。诺斯特鲁姆夫人
多洛尔·萨皮恩特·杜西莫斯·费雷尔·阿库萨莫斯·伊洛·自由,维罗·尼希尔。
显示更多
隐藏
const my段落=document.getElementById(“my段落”);
const btnShowMore=document.getElementById(“btnShowMore”);
const btnHide=document.getElementById(“btnHide”);
让myText=myparagration.innerHTML;
btnShowMore.addEventListener(“单击”,()=>{
//每次用户单击“显示更多”时
myparagration.innerHTML+=myText;
})
btnHide.addEventListener(“单击”,()=>{
//如果要显示第一个文本,请使用myText;如果不想siplay任何内容,请使用“”
myparagration.innerHTML=myText;
})
您可以通过CSS使用“无显示”或“内联显示”来执行此操作,但这里您不需要它,但下面是代码:

    <script>
    const myParagraph = document.getElementById("myParagraph");
    const btnShowMore = document.getElementById("btnShowMore");
    const btnHide = document.getElementById("btnHide");

    let myText = myParagraph.innerHTML;
    btnShowMore.addEventListener("click",()=>{
        // everytime when the user click show more
        myParagraph.innerHTML += myText;
        myParagraph.style.display = "inline";
    })
    btnHide.addEventListener("click",()=>{
        // if you want to display the first text use myText if you don't want to siplay anything use ""
        myParagraph.style.display = "none":
    })
</script>

const my段落=document.getElementById(“my段落”);
const btnShowMore=document.getElementById(“btnShowMore”);
const btnHide=document.getElementById(“btnHide”);
让myText=myparagration.innerHTML;
btnShowMore.addEventListener(“单击”,()=>{
//每次用户单击“显示更多”时
myparagration.innerHTML+=myText;
myparagration.style.display=“inline”;
})
btnHide.addEventListener(“单击”,()=>{
//如果要显示第一个文本,请使用myText;如果不想siplay任何内容,请使用“”
myparagration.style.display=“无”:
})

请分享您的代码。欢迎使用SO。你可能会发现阅读这个网站对你有帮助。为了获得您问题的最佳答案,我们希望看到您已尝试先使用解决方案自己解决问题。这里有一个你可能会发现有用的。。。