我想弄清楚爲什麼e.target.getAttribute('data-text')的值在我從我的HTML到我的Backbone JS文件時變爲null。Backbone.js獲取目標屬性
HTML:
<script type="text/template" id="lesson-template">
<span id="lesson-title"><%= tracks[0].title %></span>
<select class="sel">
<% _.each(tracks, function(track) { %>
<option value = "<%= track.text %>" data-text="<%= track.title %>"><%= track.title %></option>
<% }); %>
</select>
<p id="tracktext"><%= tracks[0].text %></p>
</script>
JS:
window.LibraryLessonView = LessonView.extend({
events: {
"change .sel " : "changeText"
},
changeText: function(e) {
alert(e.target.getAttribute('data-text')); //I am getting a null value here!
document.getElementById("lesson-title").innerHTML= e.target.getAttribute('data-text'); //I am getting a null value here as well
document.getElementById("tracktext").innerHTML= e.target.value;
}
任何澄清或幫助將不勝感激!
使用你的JavaScript代碼,如果我要說var Tracks = Backbone.Collection.extend({model:Track,url:鏈接到一些JSON文件});我正在用這種方式創建曲目集合。我將如何處理該行var v = new LibraryLessonView({collection:tracks});? – Robs
@Robs:我不確定你在問什麼。你需要'var tracks = new Tracks'來創建一個集合實例,然後'tracks.fetch()'來加載數據。 –