javascript 如何从 r.fn.init(JQuery 选择器上下文)获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48110198/
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
How to get value from r.fn.init (JQuery selector context)
提问by Adriano
Hi I try to receive a value from span from table like this:
嗨,我尝试从表中的跨度接收值,如下所示:
function getValueFromSibling(this) {
var id = $(this).parent().siblings('span.childSibbling');
}
Table looks like this:
表看起来像这样:
<tr>
<td>
<button type="button" onClick="getValueFromSibling()"></button>
</td>
<td>
<span class="childSibbling">100</span>
</td>
</tr>
But I receive something like this:
但我收到这样的东西:
id = r.fn.init [prevObject: r.fn.init(1)]
id = r.fn.init [prevObject: r.fn.init(1)]
I found that is simple form of:
我发现这是简单的形式:
var jQuery = function( selector, context ){
return new jQuery.fn.init( selector, context );
};
So there is the question. How to receive InnerHTMLfrom <span>, or how to convert r.fn.init [prevObject: r.fn.init(1)]to value?
那么问题来了。如何接收InnerHTML从<span>,或如何转换r.fn.init [prevObject: r.fn.init(1)]价值?
var result = id.val();and var result = id.get();dosen't work
var result = id.val();和 var 结果 =id.get();不起作用
回答by bipen
First of all, thisinside your function getValueFromSiblingis in window scope pass thiscontext when calling the function
首先,在调用函数时,this您的函数getValueFromSibling在窗口范围内传递this上下文
onClick="getValueFromSibling(this)"
And there are few things missing like text()to get the text you want. Try one below
并且缺少一些东西,例如text()获取您想要的文本。试试下面的一个
Better way (actually recommended way):
更好的方法(实际上是推荐的方法):
Html
html
<tr>
<td>
<button type="button" class="some-button"></button>
</td>
<td>
<span class="sibbling">100</span>
</td>
</tr>
Jquery
查询
$(function(){
$('.some-button').click(function(){
var id = $(this).closest('tr').find('.sibbling').text();
// .closest() gets the closest tr parent .
// .find() finds the element with class 'sibbling' inside that `tr`
// .text() gets the text inside the element.
});
});
回答by Kwun Yeung
You should be able to get the text value by using
您应该能够通过使用获取文本值
$("selector").text();
回答by frikinside
The problem you are facing is that you don't want a sibling, you want a child of a sibling.
你面临的问题是你不想要一个兄弟姐妹,你想要一个兄弟姐妹的孩子。
function getValueFromSibling(me) {
var id = $(me).parent().next().find('span.childSibbling').text();
console.log(id);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<table>
<tr>
<td>
<button type="button" onClick="getValueFromSibling(this)"></button>
</td>
<td>
<span class="childSibbling">100</span>
</td>
</tr>
</table>
</body>
With this, first we do a .parent()to move us on the tdelement. Then, we move on the next tdelement with .next()and finally, recover the span.childSibblingwith find('span.childSibbling').
Using the method .text()for recover the innerTextof the node or .html()for the innerHTML of the node.
有了这个,首先我们做一个.parent()在td元素上移动我们。然后,我们继续下一个td元素 with .next(),最后,恢复span.childSibblingwith find('span.childSibbling')。使用方法.text()来恢复innerText节点的或节点.html()的innerHTML。
For the r.fn.initquestion, there is an excelent answer in this question.
对于r.fn.init问题,还有在外观极好回答这个questioñ。

