javascript jquery中的toString方法?或类似的东西

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5037369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:44:32  来源:igfitidea点击:

toString method in jquery? or something similar

javascriptjquerystringreturntostring

提问by Phil

ANSWER FOUND!

找到答案!

I'm reading an xml document and I am sending this to a function

我正在阅读一个 xml 文档,并将其发送给一个函数

$(data).find('colleges').attr('next')

that returns disciplineand javascript is thinking it's a variable, can I some how get the attribute in the tag and have it return as a string like 'discipline'

返回discipline并且javascript认为它是一个变量,我可以知道如何获取标签中的属性并将其作为字符串返回'discipline'

Is there a toString()-type method that I could add onto the selection?

是否有可以添加到选择中的 toString() 类型的方法?

$(data).find('colleges').attr('next').toString()

The issue I am having is that I am sending that to a function

我遇到的问题是我将它发送到一个函数

createSelect( $(data).find('colleges').attr('next') )

but firebug is giving me an error saying that the value, discipline, is undefined? Why is javascript reading discipline as a var and not a string?

但是萤火虫给了我一个错误,说价值,纪律,是未定义的?为什么 javascript 阅读规则是一个 var 而不是一个字符串?

回答by SLaks

The attrfunction returns a string, which you can put into a variable.

attr函数返回一个字符串,您可以将其放入一个变量中。

回答by user113716

If this...

如果这...

$(data).find('colleges')

...doesn't find any matches ( <colleges>...</colleges>), then this...

...没有找到任何匹配项 ( <colleges>...</colleges>),那么这个...

.attr('next')

...will return undefined.

……会回来的undefined

The first thing you should do is test to see if the .find()found anything.

您应该做的第一件事是测试是否.find()找到了任何东西。

alert( $(data).find('colleges').length );

If the alertgives you 0, then there were no matches, and you'll have to inspect your datato see if it contains what you expect.

如果alert给你0,那么就没有匹配项,你必须检查你的data,看看它是否包含你所期望的。