如何使用jQuery选择锚标记内的文本

Vis*_*swa 2 html jquery dynatree jquery-dynatree

我想选择a标签并在点击时显示其文本.

换句话说,当我点击第一个链接 - "One"时,我想使用显示其文本"One" alert.

当我单击第二个链接 - "示例"时,我想使用显示文本"示例" alert.

<body>
    <div id="tree">
        <ul>
        <li><a target="_blank" href="one.html">One</a></li>
        <li class="folder expnded"><a target="_blank" href="two.html">Examples</a></li>
        </ul>
    </div>    
    <div id="display"></div>    
</body>
Run Code Online (Sandbox Code Playgroud)

更新1:

感谢大家的回答.我真正想要做的是我需要创建一个树结构,当我单击树叶节点时,我必须显示该叶节点的名称.

我使用jQuery DynaTree创建了树结构,但jQuery选择器在上面的代码中对我不起作用.

我无法在标签内选择标签或任何其他元素div.

下面是树形结构:

在此输入图像描述

这是我的完整HTML代码(上面只是一个示例代码)

<html>
<head>
<!-- Include the required JavaScript libraries: -->
<script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src='js/jquery-ui-1.8.20.custom.min.js' type="text/javascript"></script>
<script src='js/myjquery.js' type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="css/ui.dynatree.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="js/jquery.dynatree.js" type="text/javascript"></script>

</head>

<body>
    <div id="tree">
        <ul>
            <li>one</li>
            <li><a target="_blank" href="">Google</a>
            <li class="folder expnded">Examples
                <ul>
                    <li><a target="content" href="" id="s">one</a>
                    <li><a target="content" href="two.html">two</a>
                    <li class="folder">Folder one
                        <ul>
                            <li><a target="content" href="one.html">one</a>
                            <li><a target="content" href="two.html">two</a>
                        </ul>
                    <li><a target="content" href="three.html">three</a>
                    <li><a target="content" href="four.html">four</a>
                    <li class="folder">Folder two
                        <ul>
                            <li><a target="content" href="one.html">one</a>
                            <li><a target="content" href="two.html">two</a>
                        </ul>
                    <li class="folder">Folder three
                        <ul>
                            <li><a target="content" href="one.html">one</a>
                            <li><a target="content" href="two.html">two</a>
                            <li class="folder">Inner Folder
                                <ul>
                                    <li><a target="content" href="one.html">one</a>
                                    <li><a target="content" href="two.html">two</a>
                                </ul>
                        </ul>
                </ul>
        </ul>
    </div>

    <div id="display">
    <a href="" id="s">one</a>
    </div>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我发布的图片是上面HTML代码的输出.

在myjquery.js文件中,我有这样的代码(旧)

$(function() {
        // --- Initialize sample trees
        $("#tree").dynatree({
            autoFocus : true,
            //          persist: true,
            minExpandLevel : 2,
            onFocus : function(node) {
                // Auto-activate focused node after 1 second
                if (node.data.href) {
                    node.scheduleAction("activate", 1000);
                }
            },
            onBlur : function(node) {
                node.scheduleAction("cancel");
            },
            onActivate : function(node) {
                if (node.data.href) {
                    window.open(node.data.href, node.data.target);
                }
            }
        });
    });
Run Code Online (Sandbox Code Playgroud)

更新2:

解决我的问题

在myjquery.js文件中,以下新代码适用于我.您可以将其与上面的旧代码进行比较.

$(function() {
    // --- Initialize sample trees
    $("#tree").dynatree({
        autoFocus : true,
         persist: true,
        minExpandLevel : 1,
        onFocus : function(node) {
            // Auto-activate focused node after 1 second
            if (node.data.href) {
                node.scheduleAction("activate", 1000);
            }
        },
        onBlur : function(node) {
            node.scheduleAction("cancel");
        },
        onActivate : function(node) {
            if (node.data.href) {
                window.open(node.data.href, node.data.target);
            }
        },
        onClick : function(node) {
            $.ajax({
                type : "GET",
                url : "Display",
                data : {
                    id : node.data.title
                },
                success : function(data) {
                    $("#display").html(data);
                }
            });
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

the*_*dox 6

$('a', '#tree li').on('click', function(e) {
  e.preventDefault();    // this line is for prevent page reload
  alert($(this).text());
});
Run Code Online (Sandbox Code Playgroud)

DEMO

阅读更多关于.text()和jQuery选择器的信息

根据编辑

您可以尝试如下:

$('span[class^=dynatree-exp-c] a').on('click', function(e) {
    e.preventDefault();
    alert( $(this).text() );
});
Run Code Online (Sandbox Code Playgroud)

dynatree的每个叶节点跨度有类像dynatree-exp-c,dynatree-exp-c1等等,所以我使用[class^=dynatree-exp-c]来选择范围,它的类开始dynatree-exp-c.

  • @Viswa:在发布问题之前你是谷歌吗? (2认同)