2017-03-20 7 views
0

私のプロジェクトでは何をしようとしています。 divタグで囲まれた単純なコンテンツボックスがあります。追加された段落があり、独自のidを持っています。私はマージン、パディングなどを設定しました。次に、一番下に、典型的な送信ボタンを追加しました。私がラベルを貼ったのは、それ以上のものでした。コンテンツボックス内でオーバーフローが発生すると、残りのテキストを表示できるように高さを増やしたいと考えています。しかし、if/elseステートメントで匿名関数を使用してonclickコマンドを追加すると、問題が発生する可能性があります。送信ボタンのonclick機能はどうなっていますか?

ここにHTMLコードがあります。何が起こっている

var content = document.getElementById('contentbox'); 
 
    var button = document.getElementById('showMore'); 
 
    
 
    button.onclick = function() { 
 
    \t if(content.className == "open") { 
 
    \t \t content.className = ""; 
 
    \t } else { 
 
    \t \t content.className = "open"; 
 
    \t } 
 
    };
body { 
 
    \t padding:0; 
 
    \t margin:0; 
 
    \t font-family:sans-serif; 
 
    } 
 
    
 
    #contentbox { 
 
    \t margin:100px 300px 100px 300px; 
 
    \t border:1px solid black; 
 
    \t padding:5px 0px 5px 20px; 
 
    \t max-height:200px; 
 
    \t overflow:hidden; 
 
    \t background:silver; 
 
    \t border-radius:10px; 
 
    
 
    } 
 
    #contentbox.open { 
 
    \t max-height:1000px; 
 
    \t overflow:visible; 
 
    } 
 
    #content { 
 
    \t color:white; 
 
    \t font-size:20px; 
 
    \t text-indent:30px; 
 
    } 
 
    
 
    #showMore { 
 
    \t margin-left:auto; 
 
    \t margin-right:auto; 
 
    \t display:block; 
 
    \t background:gray; 
 
    \t font-size:40px; 
 
    \t text-transform:uppercase; 
 
    \t font-family:impact; 
 
    \t color:silver; 
 
    \t transition:all 0.5s ease-in-out; 
 
    } 
 
    #showMore:hover { 
 
    \t color:gray; 
 
    \t background:silver; 
 
    \t cursor:pointer; 
 
    }
 <html> 
 
    <head> 
 
    \t <title>Kenneth's new HTML/CSS Exercise</title> 
 
    \t <meta charset="UTF-8"> 
 
    \t <link rel='stylesheet' type='text/css' href='contentbox.css'> 
 
    \t <script src="stuff.js"></script> 
 
    </head> 
 
    <body> 
 
    <div id="contentbox"> 
 
    <p id="content"> 
 
    this is a bunch of text for you to read and Game of Thrones is an American fantasy drama television series created by David Benioff and D. B. Weiss. It is an adaptation of A Song of Ice and Fire, George R. R. Martin's series of fantasy novels, the first of which is A Game of Thrones. It is filmed at Titanic Studios in Belfast, on location in the United Kingdom, and in Croatia, Iceland, Malta, Morocco, Spain, and the United States. The series premiered on HBO in the United States on April 17, 2011, and its sixth season ended on June 26, 2016. The series was renewed for a seventh season,[1] which is scheduled to premiere on July 16, 2017.[2] The series will conclude with its eighth season in 2018 </p> 
 
    
 
    <p id='content'> Set on the fictional continents of Westeros and Essos, Game of Thrones has several plot lines and a large ensemble cast. The first story arc follows a dynastic conflict among competing claimants for succession to the Iron Throne of the Seven Kingdoms, with other noble families fighting for independence from the throne. The second covers attempts to reclaim the throne by the exiled last scion of the realm's deposed ruling dynasty; the third chronicles the threat of the impending winter and the legendary creatures and fierce peoples of the North. </p> 
 
    
 
    <p id='content'> Game of Thrones has attracted record viewership on HBO and has a broad, active, international fan base. It has been acclaimed by critics, particularly for its acting, complex characters, story, scope, and production values, although its frequent use of nudity and violence (including sexual violence) has attracted criticism. The series has received 38 Primetime Emmy Awards, including Outstanding Drama Series in 2015 and 2016, more than any other primetime scripted television series. Its other awards and nominations include three Hugo Awards for Best Dramatic Presentation (2012–2014), a 2011 Peabody Award, and four nominations for the Golden Globe Award for Best Television Series – Drama (2012 and 2015–2017). Of the ensemble cast, Peter Dinklage has won two Primetime Emmy Awards for Outstanding Supporting Actor in a Drama Series (2011 and 2015) and the Golden Globe Award for Best Supporting Actor – Series, Miniseries or Television Film (2012) for his performance as Tyrion Lannister. Lena Headey, Emilia Clarke, Kit Harington, Maisie Williams, Diana Rigg, and Max von Sydow have also received Primetime Emmy Award nominations for their performances in the series. </p> 
 
    
 
    </div> 
 
    
 
    <input type='submit' value='Show more' id="showMore"></input> 
 
    
 
    </body> 
 
    </html> 
 

 

 
    
 

 

と私はそれを修正するためにしなければならないのですか?

+1

送信ボタンを投稿 – epascarello

+0

ハァッ?あなたはどういう意味ですか? –

+0

送信ボタンは、ページの状態がリセットされるようにページを送信します。 – epascarello

答えて

0

はこれを行うにしてください:

var content = document.getElementById('contentbox'); 
var button = document.getElementById('showMore'); 

button.onclick = function(e) { 
    e.preventDefault(); 
    //do your logic 
    if(content.className == "open") { 
     content.className = ""; 
    } else { 
     content.className = "open"; 
    } 
    // 
}; 

あなたのロジックをスキップイベントを提出防ぐそのように。 しかし、この場合はすべての操作の後に手動でフォームを送信する必要があります。高さではない最大の高さ へ

0

に役立つ何が起こっているかを願っています。

.openスタイルオーバーフロー:可視プロパティは、ボックスの最大高さの後でもテキストがブラウザによって描画され続けるようにします。あなたはおそらくそれを望んでいません。

これを行う場合、高さはコンテンツに基づいて計算されます。

#contentbox.open { 
    height: auto; 
    max-height: initial; 
} 

Working jsfiddle

あなたはスクロール行動(通常)を変更する場合を除き、overflowプロパティを変更する必要はありません。最大高さの初期値は最大高さを上書きします。

#contentbox.open { 
    overflow: auto; 
} 

をそして、あなたはまだしたい場合、あなたは、max-高さを変更することができます

かわりにスクロールバーをしたい場合は、これを行います。

これは良い説明でした。


他のユーザーは、あなたのボタンが送信ボタンであると指摘しています。これはページロードを引き起こし、何かしようとしているものを無効にする可能性があるため、問題を引き起こす可能性があります。前のjsfiddleリンクでは、ボタンはフォームを送信しないので、コードはそのまま動作します(falseを返す、ボタンタイプを変更する、または伝播を停止することはありません)。

しかし、paste binのようなフォームを送信していない場合は、クリックハンドラをfalseに戻して、jsfiddleのようなオールインワンのソリューションに変更できます。

ボタンからtype='submit'を削除しても問題ありません。

+0

何らかの理由でまったく動作しませんでした。他に働くことはありますか? –

+0

それはjsfiddleで私のために働いた。デバッガ(F12)を見て、要素を調べて計算されたスタイルを確認します。何かがそれを上書きしているかもしれない。私は実際の解決策のjsのバイブルを取得します。 –

+0

追加されました。 –

関連する問題