jQuery simple value get issue with .val()

牧云@^-^@ 提交于 2020-01-24 17:32:09

问题


I have the following code:

$('.document').ready(function(){
   alert($('font.someClass').val());
});

Here is a Fiddle with it.

Does anyone know why I can not return the value of a font tag?

Am I to assume that you are not allowed to call the value or set the value of a font tag.


回答1:


.text() not .val() - .val() is for form elements.




回答2:


The val() method get values from value which exists for form elements (like input). You is looking to get the text from some DOM element, so use the text() method.

I updated your Fiddle here with the code:

$(document).ready(function() {
   alert($("font.someClass").text());
});



回答3:


This will get you the text:

$('.document').ready(function(){
 alert($('font.someClass').text());
});


来源:https://stackoverflow.com/questions/6178827/jquery-simple-value-get-issue-with-val

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!