jquery ie8 get text value = Object doesn't support this property or method

拟墨画扇 提交于 2019-12-24 03:24:30

问题


I have two span tags inside two divs inside a master div

<div>
  <div>
    <span id="spanA">start text</span>
  </div>
  <div>
    <span id="spanB">start text two</span>
 </div>
<div>

I use jquery 1.7.2 and am trying to get the default text value on the page load for span a & b, then onclick change the value of span a & b and then on toggle change them back. On FF/chrome this works fine but ie8 is throwing an error

Object doesn't support this property or method 

onload

spanAval = $('#spanA').text();
spanBval = $('#spanB').text();

onclick

$('#spanA').text("replace text");
$('#spanB').text("replace text two");

onToggle

$('#spanA').text(spanAval);
$('#spanB').text(spanBval);

in ie8 the text is replaced with "[object]"

I am sure I am missing something basic, thanks Art


回答1:


ok it was something basic, I had the same name for the var as the id

spanA = $('#spanA').text();

and it failed in ie8 but worked in the other browsers FF/chrome etc , by changing the var name to a unique name it works in ie8

spanAval = $('#spanA').text();

thx for the quick replies




回答2:


the last div is not closed. Jquery have several bugs as well.




回答3:


I think you can use plain .innerText or .textContent (in Firefox) property i.e:

var text = $('selector')[0].innerText 
             ? $('selector')[0].innerText 
             : $('selector')[0].textContent // for Firefox



回答4:


you can circumvent this error by declaring "var" before the variable in IE.

var spanA = $('#spanA').text();


来源:https://stackoverflow.com/questions/11167656/jquery-ie8-get-text-value-object-doesnt-support-this-property-or-method

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