2017-09-16 4 views
0

は私がthisのようなドロップダウンメニューがあります。クリッカブルHTMLドロップダウンメニュー

document.getElementById("example").addEventListener("click", denemeFunc); 
 

 
function denemeFunc() { 
 

 
    document.getElementById("searchBtn").innerText = ? ? ? ? ? ? ? ? ? ? ? ? ? ? 
 

 
}
<div class="dropdown"> 
 
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()"> 
 
    <div id="example" runat="server"> 
 
    <a href="#About" data-toggle="About">About</a> 
 
    <a href="#base" data-toggle="Base">Base</a> 
 
    <a href="#blog" data-toggle="Blog">Blog</a> 
 
    <a href="#contact" data-toggle="Contact">Contact</a> 
 
    <a href="#custom">Custom</a> 
 
    <a href="#support">Support</a> 
 
    <a href="#tools">Tools</a> 
 
</div>

をここに私の質問です:そのクリックされたときにどのように私は、タグの名前をキャッチすることができますか? 例:#Aboutタグをクリックしました。私はsearchBtnの名前をAboutとしたいと思います。これどうやってするの ?

+0

を経由してイベントを発射した委任要素を問い合わせることができますか? - "document.getElementById(" example ")。addEventListener(" click "、function(event){console.log(event.target)})'のようになり、ターゲットのparamからhrefを取ることができます。 – YoroDiallo

+0

htmlに '#searchBtn'はどこですか? – sol

答えて

1

まずコールバックで

function denemeFunc(evt) { 

をイベントオブジェクトを受け取るその後、あなたはaddEventListener` `に` event`を追加する方法についてevt.target

document.getElementById("searchBtn").innerText = evt.target.innerHTML;