2013-06-19 57 views
0

我有一個具有多個textareas的Web應用程序,並且可以添加更多內容(如果您願意)。新創建元素上的事件處理程序

當您將焦點從一個文本區域轉移到另一個文本區域時,焦點中的焦點會動畫變大,剩下的縮小。

當頁面加載時,它完全處理html文件中最初四個框的動畫,但是當您單擊按鈕添加更多textarea時,動畫無法容納這些新元素......也就是說,除非您將初始查詢放入函數中,並從與該按鈕綁定的addelement函數中調用該函數。

但!!當你這樣做時,它會多次查詢你添加一個新的元素。因此,如果您快速添加10個新textareas,下次您將焦點放在任何textarea上時,查詢將運行10次。

問題在我的設計或jQueries實施?如果前者,我能更好地設計它,如果是後者,我該如何解決它?

我試着把代碼砍到相關的位......我試過所有從焦點和模糊,到按鍵,最新點擊。

HTML ::

<html> 
<head> 
    <link rel="stylesheet" type="text/css" href="./sty/sty.css" />  
    <script src="./jquery.js"></script> 
<script> 
    $().ready(function() { 
     var $scrollingDiv = $("#scrollingDiv"); 

     $(window).scroll(function(){    
      $scrollingDiv 
       .stop() 
       //.animate({"marginTop": ($(window).scrollTop() + 30) + "px"}, "slow");    
       .animate({"marginTop": ($(window).scrollTop() + 30) + "px"}, "fast");   
     }); 
    }); 
</script> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>boxdforstacks</title> 
</head> 
<body> 
    <div class="grid"> 

     <div class="col-left" id="left"> 
         <div class="module" id="scrollingDiv"> 
      <input type="button" value="add" onclick="addele()" /> 
      <input type="button" value="rem" onclick="remele()" /> 
      <p class="display">The value of the text input is: </p> 
      </div> 
     </div> <!--div class="col-left"--> 


     <div class="col-midd"> 
      <div class="module" id="top"> 
          <p>boxa</p> 
          <textarea class="tecksd" placeholder="begin typing here..." id="boxa" ></textarea> 
          <p>boxb</p> 
          <textarea class="tecksd" placeholder="begin typing here..." id="boxb"></textarea> 
          <p>boxc</p> 
          <textarea class="tecksd" placeholder="begin typing here..." id="boxc"></textarea> 
          <p>boxd</p> 
          <textarea class="tecksd" placeholder="begin typing here..." id="boxd"></textarea> 
     </div> 
     </div> <!--div class="col-midd"--> 
    </div> <!--div class="grid"--> 

</body> 
</html> 
    <script type="text/javascript" src="boxd.js"></script> 

JS:

function onit(){ 
    $('textarea').on('keyup change', function() { 
     $('p.display').text('The value of the text input is: ' + $(this).val()); 
    }); 

} 
$('textarea').on("click",function(){ 
     //alert(this.id.substring(0,3)); 
     if (this.id.substring(0,3) == 'box'){ 
         $('textarea').animate({ height: "51" }, 1000); 
         $(this).animate({ height: "409" }, 1000); 
     } else { 
         $('textarea').animate({ height: "51" }, 1000); 
     } 
} 
    ); 
var boxfoc=""; 
var olebox=""; 
var numb = 0; 
onit(); 

function addele() { 

    var tops = document.getElementById('top'); 
    var num = numb + 1; 
    var romu = romanise(num); 

    var newbox = document.createElement('textarea'); 
    var newboxid = 'box'+num; 

    newbox.setAttribute('id',newboxid); 
    newbox.setAttribute('class','tecksd'); 
    newbox.setAttribute('placeholder','('+romu+')'); 

    tops.appendChild(newbox); 

    numb = num; 
    onit(); 
} //addele(), add element 




function remele(){ 

    var tops = document.getElementById('top'); 
    var boxdone = document.getElementById(boxfoc); 
    tops.removeChild(boxdone); 
} // remele(), remove element 



function romanise (num) { 
    if (!+num) 
     return false; 
    var digits = String(+num).split(""), 
     key = ["","c","cc","ccc","cd","d","dc","dcc","dccc","cm", 
       "","x","xx","xxx","xl","l","lx","lxx","lxxx","xc", 
       "","i","ii","iii","iv","v","vi","vii","viii","ix"], 
     roman = "", 
     i = 3; 
    while (i--) 
     roman = (key[+digits.pop() + (i * 10)] || "") + roman; 
    return Array(+digits.join("") + 1).join("M") + roman; 
} // romanise(), turn numbers into roman numerals 

CSS:

.tecksd { 
    width: 97%; 
    height: 51; 
    resize: none; 
    outline: none; 
    border: none; 
    font-family: "Lucida Console", Monaco, monospace; 
    font-weight: 100; 
    font-size: 70%; 
    background: white; 
/* box-shadow: 1px 2px 7px 1px #0044FF;*/ 
} 

.tecksded { 
    width: 97%; 
    resize: none; 
    outline: none; 
    border: none; 
    overflow: auto; 
    position: relative; 
    font-family: "Lucida Console", Monaco, monospace; 
    font-weight: 100; 
    font-size: 70%; 
    background: white; 
/* box-shadow: 1px 2px 7px #FFDD00;*/ 
} 


/*#postcomp { 
    width: 500px; 
}*/ 


* { 
    @include box-sizing(border-box); 
} 

$pad: 20px; 

.grid { 
    background: white; 
    margin: 0 0 $pad 0; 

    &:after { 
    /* Or @extend clearfix */ 
    content: ""; 
    display: table; 
    clear: both; 
    } 
} 

[class*='col-'] { 
    float: left; 
    padding-right: $pad; 
    .grid &:last-of-type { 
    padding-right: 0; 
    } 
} 
.col-left { 
    width: 13%; 
} 
.col-midd { 
    width: 43%; 
} 
.col-rght { 
    width: 43%; 
} 

.module { 
    padding: $pad; 


} 

/* Opt-in outside padding */ 
.grid-pad { 
    padding: $pad 0 $pad $pad; 
    [class*='col-']:last-of-type { 
    padding-right: $pad; 
    } 
} 

body { 
    padding: 10px 50px 200px; 
    background: #FFFFFF; 
    background-image: url('./backgrid.png'); 

} 
h1 { 
    color: black; 
    font-size: 11px; 
    font-family: "Lucida Console", Monaco, monospace; 
    font-weight: 100; 
} 
p { 
    color: white; 
    font-size: 11px; 
    font-family: "Lucida Console", Monaco, monospace; 
    font-weight: 100; 
} 

回答

2

你應該我們E以下:

// New way (jQuery 1.7+) - .on(events, selector, handler) 
$(document).on("click", "textarea", function() { 
    event.preventDefault(); 
    alert('testlink'); 
}); 

由於textarea動態添加,你需要使用event delegation註冊事件處理程序。

+1

謝謝你,活動代表團鏈接非常感謝 – Peregrine

+0

@Peregrine不客氣! –

1

嘗試

$(document).on('click', 'textarea', function() { 
    // do something 
}); 
0

問題是你綁定textareas只在頁面加載。我做了一個的jsfiddle與工作代碼:http://jsfiddle.net/VpABC/

這是我改變了: 我包:

$('textarea').on("click", function() { 
     //alert(this.id.substring(0,3)); 
     if (this.id.substring(0, 3) == 'box') { 
      $('textarea').animate({ 
       height: "51" 
      }, 1000); 
      $(this).animate({ 
       height: "409" 
      }, 1000); 
     } else { 
      $('textarea').animate({ 
       height: "51" 
      }, 1000); 
     } 
    }); 

的功能,所以它看起來是這樣的:

function bindTextAreas() { 
    $('textarea').unbind("click"); 
    $('textarea').on("click", function() { 
     //alert(this.id.substring(0,3)); 
     if (this.id.substring(0, 3) == 'box') { 
      $('textarea').animate({ 
       height: "51" 
      }, 1000); 
      $(this).animate({ 
       height: "409" 
      }, 1000); 
     } else { 
      $('textarea').animate({ 
       height: "51" 
      }, 1000); 
     } 
    }); 
} 
bindTextAreas(); 

這樣做是它允許您在創建新的textarea時調用此函數bindTextAreas。這將解除所有當前事件而不是重新綁定它們。這將使你的新textarea具有點擊處理程序設置。

的地方調用這個函數是在這樣的addele功能:

function addele() { 

    var tops = document.getElementById('top'); 
    var num = numb + 1; 
    var romu = romanise(num); 

    var newbox = document.createElement('textarea'); 
    var newboxid = 'box' + num; 

    newbox.setAttribute('id', newboxid); 
    newbox.setAttribute('class', 'tecksd'); 
    newbox.setAttribute('placeholder', '(' + romu + ')'); 

    tops.appendChild(newbox); 

    numb = num; 
    onit(); 
    bindTextAreas(); 
} //addele(), add element 

通知的bindTextAreas();線附近的底部。這會重新加載所有的點擊處理程序。

相關問題