Esempio n. 1
0
        /// <summary>
        /// 获取CSS Selector(当包含上下文的时候调用此方法)
        /// http://stackoverflow.com/questions/3390396/how-to-check-for-undefined-in-javascript
        /// </summary>
        /// <param name="element"></param>
        public string GetContextSelector(mshtml.IHTMLElement context, mshtml.IHTMLElement element)
        {
            SetContextSelector(context);
            var originalId = element.id;

            if (string.IsNullOrEmpty(originalId))
            {
                var id = "__" + IEUtils.IEVariableNameHelper.CreateVariableName();
                element.id = id;
            }
            mshtml.IHTMLWindow2 parentWindow = ((IHTMLDocument2)(element.document)).parentWindow as mshtml.IHTMLWindow2;
            IEUtils.RunScript(ScriptLoader.GetJquerySelector(""), parentWindow);
            StringBuilder sb = new StringBuilder();

            sb.Append(string.Format("var el = $jq('#{0}');", element.id));
            sb.AppendLine();

            sb.Append("if(typeof __context == 'undefined'){");
            sb.AppendLine();
            sb.Append("__context = '';");
            sb.AppendLine();
            sb.Append("}");
            sb.AppendLine();
            sb.Append(" var selector = el.getCssPath(__context);");
            sb.AppendLine();
            sb.Append("__context = 'undefined';");
            sb.Append("return selector");
            object result = JS.FunEval(AppContext.Current.Window, sb.ToString());

            element.id = originalId;
            return(result == null ? "" : result.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// 获取CSS Selector(当包含上下文的时候调用此方法)
        /// http://stackoverflow.com/questions/3390396/how-to-check-for-undefined-in-javascript
        /// </summary>
        /// <param name="element"></param>
        public void SetSelector(mshtml.IHTMLElement element)
        {
            var originalId = element.id;

            if (string.IsNullOrEmpty(originalId))
            {
                var id = "__" + IEUtils.IEVariableNameHelper.CreateVariableName();
                element.id = id;
            }
            mshtml.IHTMLWindow2 parentWindow = ((IHTMLDocument2)(element.document)).parentWindow as mshtml.IHTMLWindow2;
            IEUtils.RunScript(ScriptLoader.GetJquerySelector(""), parentWindow);
            StringBuilder sb = new StringBuilder();

            sb.Append(string.Format("var el = $jq('#{0}');", element.id));
            sb.AppendLine();

            sb.Append("if(typeof __context == 'undefined'){");
            sb.AppendLine();
            sb.Append("__context = '';");
            sb.AppendLine();
            sb.Append("}");
            sb.AppendLine();
            sb.Append(" var selector = el.getCssPath(__context);");
            sb.AppendLine();

            sb.Append("el.attr('__selector', selector);");
            //sb.Append("alert(el.attr('__selector'));");
            //sb.AppendLine();

            IEUtils.RunScript(sb.ToString(), parentWindow);
            element.id = originalId;
        }