【问题标题】:C# Remove the underline under a link when selecting more than the linkC#选择多个链接时删除链接下的下划线
【发布时间】:2015-09-09 02:31:52
【问题描述】:

当我同时选择链接和文本时,我试图删除链接下的下划线。我知道element = element.children 不起作用,但我找不到办法。

    private void UnderlineExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        if (htmldoc != null)
        {
            htmldoc.Underline();
            IHTMLSelectionObject selec = htmldoc.GetSelection();
            IHTMLElement element = null;
            IHTMLTxtRange txtRange = (IHTMLTxtRange)htmldoc.GetIHTMLDocument2().selection.createRange();
            element = txtRange.parentElement();


            while (element != null
                && !(element.tagName.Equals("A", StringComparison.InvariantCultureIgnoreCase)))
            {
                if (element.tagName.Equals("A"))
                {
                    element.style.setAttribute("text-decoration", "none");
                }
                element = element.children;
            }
        }
    }

知道所选范围的 HTML 文本是一个 <U> 标记、一个 <A> 标记和另一个 <U> 标记。

感谢您的回答!

【问题讨论】:

  • 为什么不用客户端 CSS 做这个?
  • 也不知道如何使用 CSS。

标签: c# html underline


【解决方案1】:

我发现解决方案很简单。我没有找到正确的方法。 我不知道我们可以用 IHTMLElement 创建一个 IHTMLElementCollection。 这是我的代码:

    private void UnderlineExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        if (htmldoc != null)
        {
            bool underline = false;
            htmldoc.Underline();
            IHTMLSelectionObject selec = htmldoc.GetSelection();
            IHTMLElement element = null;
            IHTMLTxtRange txtRange = (IHTMLTxtRange)htmldoc.GetIHTMLDocument2().selection.createRange();
            element = txtRange.parentElement();
            if (element.tagName.Equals("A"))
            {
                element.style.setAttribute("text-decoration", "none");
            }
            IHTMLElementCollection children = element.all;
            foreach (IHTMLElement child in children)
            {
                if (child.tagName.Equals("U"))
                {
                    underline = true;
                }
            }
            foreach (IHTMLElement child in children)
            {
                if (child.tagName.Equals("A") && underline == false)
                {
                    child.style.setAttribute("text-decoration", "none");
                }
            }
        }

【讨论】:

    猜你喜欢
    • 2013-11-10
    • 2015-11-10
    • 2015-02-26
    • 1970-01-01
    • 2012-03-06
    • 2014-07-06
    • 1970-01-01
    • 2011-02-16
    相关资源
    最近更新 更多