在选择器失败之前,我遇到了类似的问题,直到我这样做:
var stuff = '<input id="testinput" name="testinput" type="text" value="this is the one" /><input id="testinput2" name="testinput2" type="text" value="this is the wrong one" />';
var myValue = HtmlElementAttribute(stuff, 'testinput2', 'value');
function HtmlElementAttribute(htmlString, elementId, attributeName) {
var foundValue = '';
$(htmlString).filter(function (x) {
var idX = eval('$(htmlString)[x].id');
if (idX && idX == elementId) {
foundValue = $(eval('$(htmlString)[x].outerHTML')).attr(attributeName);
}
});
return foundValue;
}