问题
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