获取javascript绑定字符串值
我有一个简单的函数,它遍历DOM以获得span元素:获取javascript绑定字符串值,javascript,Javascript,我有一个简单的函数,它遍历DOM以获得span元素: function getAttachmentControlBinding(theNode) { //Find the <SPAN> tag that has the binding to the attachment control //The attachment control is needed to be able to determine from which view do { //Navigate
function getAttachmentControlBinding(theNode) {
//Find the <SPAN> tag that has the binding to the attachment control
//The attachment control is needed to be able to determine from which view
do {
//Navigate up to the parent node
theNode = theNode.parentNode;
//Should run into a <SPAN> tag, but if not we need to get out of the loop
} while (theNode.nodeName !== "SPAN" && theNode.nodeName !== '');
//Get the value of the binding attribute from the <SPAN> tag
return theNode.getAttribute("binding");
}
函数getAttachmentControlBinding(theNode){
//查找绑定到附件控件的标记
//附件控件需要能够从哪个视图确定
做{
//向上导航到父节点
theNode=theNode.parentNode;
//应该会遇到标记,但如果不是,我们需要退出循环
}while(theNode.nodeName!=“SPAN”&&theNode.nodeName!=“SPAN”);
//从标记中获取绑定属性的值
返回node.getAttribute(“绑定”);
}
在JS控制台中,如果我查看节点
,我会发现:
您的代码运行良好
函数getAttachmentControlBinding(theNode){
//查找绑定到附件控件的标记
//附件控件需要能够从哪个视图确定
做{
//向上导航到父节点
theNode=theNode.parentNode;
//应该会遇到标记,但如果不是,我们需要退出循环
}while(theNode.nodeName!=“SPAN”&&theNode.nodeName!=“SPAN”);
//从标记中获取绑定属性的值
返回node.getAttribute(“绑定”);
}
var binding=getAttachmentControlBinding(document.querySelector('.nesteddiv2');
document.querySelector('.js result')。innerHTML='找到的绑定:'+绑定代码>
aa
您能在JSFIDLE中复制它吗?当我尝试它时,它对我来说很好。是的,我明白了…为什么我会得到我得到的错误?@stphnlwlsh如果没有一个演示,我们无法帮助你。你放在一起的演示正是我正在经历的。这个js是从另一个库中运行的,我感觉它在那里的某个地方出错了。谢谢您的帮助。@stphnlwlsh我唯一的猜测是您试图在函数之外使用“return”-我不知道为什么您会得到非法的return语句