[英]cannot get string inside an element - object literal javascript
I'm trying to to get the strings of two divs .class-date
and .class-time
but i'm getting an Uncaught TypeError "e.siblings is not a function". 我正在尝试获取两个div .class-date
和.class-time
的字符串,但是我遇到了Uncaught TypeError“ e.siblings不是一个函数”。 This probably a rudimentary mistake but I can't work it out. 这可能是一个基本错误,但我无法解决。 Any pointers in the right direction would be greatly appreciated . 正确方向的任何指针将不胜感激。
var Cc = { init: function() { Cc.bindEvent(); }, bindEvent: function() { $('.book-class').on("click", function() { let selectedclass = this; Cc.parseClass(selectedclass); }); }, parseClass: function(selectedclass) { let classdate = selectedclass.siblings('.class-date').text(); let classtime = selectedclass.siblings('.class-time').text(); console.log(classdate, classtime); } } jQuery(document).ready(function($) { Cc.init(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="class"> <div class="class-date">Thursday, 1 January</div> <div class="class-time">18:00 — 20:00</div> <div class="book-class">Book Class</div> </div>
Inside the click
callback function, this
is a HTMLElement and you're treating it like a jQuery object. 在click
回调函数内部, this
是一个HTMLElement,您将其像jQuery对象一样对待。
Change let selectedclass = this;
更改let selectedclass = this;
to let selectedclass = $(this);
let selectedclass = $(this);
this
is not a jQuery object. this
不是jQuery对象。 It is the DOM object of the clicked item. 它是单击项的DOM对象。
Use $(this)
and to make it even clearer, add a $
on the vars to show you expect and process a jQuery object 使用$(this)
并使其更加清晰,在vars上添加一个$
以显示您的期望并处理jQuery对象
var Cc = { init: function() { Cc.bindEvent(); }, bindEvent: function() { $('.book-class').on("click", function() { let $selectedclass = $(this); Cc.parseClass($selectedclass); }); }, parseClass: function($selectedclass) { let classdate = $selectedclass.siblings('.class-date').text(); let classtime = $selectedclass.siblings('.class-time').text(); console.log(classdate, classtime); } } $(function() { Cc.init(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="class"> <div class="class-date">Thursday, 1 January</div> <div class="class-time">18:00 — 20:00</div> <div class="book-class">Book Class</div> </div>
You could also skip the anonymous function in bindEvent
click handler and pass CC.parseClass
as reference instead. 您也可以跳过bindEvent
单击处理程序中的匿名函数,而改为传递CC.parseClass
作为引用。 It will change the context of this
inside parseClass
to the element the event occurred on which may or may not be confusing regarding using this
它将改变的背景下this
里面parseClass
到事件发生的元素可能会或可能不会被混淆关于使用this
var Cc = {
init: function() {
Cc.bindEvent();
},
bindEvent: function() {
$('.book-class').on("click", Cc.parseClass);
},
parseClass: function(event) {
// `this` is element from event handler
let $selectedclass = $(this);
let classdate = $selectedclass.siblings('.class-date').text();
let classtime = $selectedclass.siblings('.class-time').text();
console.log(classdate, classtime);
}
}
$(function() {
Cc.init();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.