Javascript 如何选择作为Jquery参数传入的DOM元素

Javascript 如何选择作为Jquery参数传入的DOM元素,javascript,jquery,Javascript,Jquery,所以我有 <span onclick="show(this)">foobar</span> function show(theSpan) { ....... //select the span here } foobar 功能显示(SPAN){ ……此处选择跨度 } 如何使用Jquery选择器来选择范围?我试过$(theSpan),但不起作用。我想选择它作为Jquery对象,以便对其应用attr() 谢谢 $(theSpan.attr('test,'123'

所以我有

<span onclick="show(this)">foobar</span>

function show(theSpan) {
    ....... //select the span here
}
foobar
功能显示(SPAN){
……此处选择跨度
}
如何使用Jquery选择器来选择范围?我试过$(theSpan),但不起作用。我想选择它作为Jquery对象,以便对其应用attr()

谢谢

$(theSpan.attr('test,'123')工作正常


你的问题在别的地方。

好吧,你的元素中没有阿曲布他,但举个例子,假设你得到了:

<span id="spTest" onclick="show(this);">foobar</span>​
但我不确定你的问题是什么意思,也许你需要价值,所以这是获得价值的方法:

var spanValue = $(theSpan).html(); //outputs: foobar
此外,如果您试图为该元素设置id,则可以执行以下操作:

//Set the id
$(theSpan).attr('id', 'theSpan');
//Store the value from your element by the new id
var spanValue = $('#theSpan').html();
//Do what you want at this point
alert(spanValue); //outputs: foobar

你所说的“不起作用”是什么意思。它是否抛出任何错误?您希望从span中得到什么属性?是$(theSpan.attr('test,'123');工作正常。问题出在其他地方,请投票赞成删除。谢谢大家。抱歉搞混了。我以为选择器不起作用,结果是其他原因
//Set the id
$(theSpan).attr('id', 'theSpan');
//Store the value from your element by the new id
var spanValue = $('#theSpan').html();
//Do what you want at this point
alert(spanValue); //outputs: foobar