Jquery 委派事件和$(this.val())

Jquery 委派事件和$(this.val()),jquery,console,html,Jquery,Console,Html,我目前拥有以下代码: $("body").on("click", ".firstClass, .secondClass", function() { if ($(this).prop("class") == "firstClass") { //Something } else if ($(this).prop("class") == "secondClass") { console.log($(this).prop("class"));

我目前拥有以下代码:

$("body").on("click", ".firstClass, .secondClass", function() {
     if ($(this).prop("class") == "firstClass") {
         //Something
     } else if ($(this).prop("class") == "secondClass") {
         console.log($(this).prop("class"));
         var variable = $(this).val();
         console.log(variable);
     }
});
第一个
console.log()。我想这与委托侦听器事件有关,但是我不明白的是为什么在第一个实例中正确引用了
$(this)
,而在第二个实例中没有

我希望在这两种情况下都能成功回归,但情况显然并非如此。为什么?

HTML格式如下:

<section>
    <h3>Heading</h3>
    <div>
        <div>
            <!-- .firstClass elements are dynamically created here within dynamically created div -->
            <div>
                <span class="firstClass">Some value</span>
                <span class="firstClass">Another value</span>
                ...
            </div>
        </div>
    </div>
    <section>
        <h4>Heading</h4>
        <div>
            <!-- .secondClass elements are dynamically created here -->
            <span class="secondClass">Some value</span>
            <span class="secondClass">Another value</span>
            ...
        </div>
    </section>
</section>

标题
一些价值
另一个价值
...
标题
一些价值
另一个价值
...

您尝试获取的不是“值”,因为
span
没有值(输入有值),而是span的HTML内容

使用


你的元素有值吗?@它是动态创建的,如果有区别的话?但如果这是问题的话,我本以为委托的侦听器会解决这个问题。@tuChargupta HTML添加了。@dystroy在控制台中,如果我对元素(
console.log($(this))
)运行查询,它会返回给我
,但如果我像上面那样运行值查询(
console.log($(this.val())
)其中的
span
我得到了
返回。真是个白痴。。。我对这里的简单错误视而不见。非常感谢。
var variable = $(this).html();
console.log(variable);
var variable = $(this).text();
console.log(variable);