每次用户点击链接时使用javascript获取属性的值

时间:2018-02-08 00:50:27

标签: javascript jquery html tags

我有一个导航菜单,但我打算获得"标题"属于每次在javascript中用户点击的每个链接。我使用jquery尝试了下面的函数,但我没有看到它似乎失败的地方。非常感谢任何帮助。



 Fread = FullRead(sock,&bet2,sizeof(bet2));  // writes new value into bet2!

     jquery(function() {
        var title = jquery(title).attr('title');    
        console.log("you have selected" + "" title);
    });




结果:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a title = "google 1" href="https://www.google.com">Visit google</a>
    <a title = "Yahoo" href="https://www.yahoo.com">Visit This link</a>
    <a title = "Verizon Wireless" href="https://www.Verizon.com">Visit Verizon</a>
    <a title = "Social Media" href="https://www.facebook.com">Visit facebook/a>

2 个答案:

答案 0 :(得分:0)

首先,您的代码中有两个拼写错误:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/progress"
    android:top="16dp"
    android:bottom="16dp">
    <layer-list>
        <item>
            <rotate
                android:fromDegrees="0"
                android:pivotX="50%"
                android:pivotY="50%"
                android:toDegrees="360">
                <shape  android:shape="ring"
                        android:innerRadiusRatio="2.3"
                        android:thickness="3.8sp"
                        android:useLevel="true">
                <solid
                        android:width="1dp"
                        android:color="#79bac6" />
                </shape>
            </rotate>
        </item>
        <item>
            <rotate
                android:fromDegrees="90"
                android:pivotX="50%"
                android:pivotY="50%"
                android:toDegrees="180">
                <shape  android:shape="ring"
                    android:innerRadiusRatio="2.3"
                    android:thickness="3.8sp"
                    android:useLevel="true">
                    <solid
                    android:width="1dp"
                    android:color="#7985c6" />
                </shape>
            </rotate>
        </item>
        <item>
            <rotate
                android:fromDegrees="360"
                android:pivotX="50%"
                android:pivotY="50%"
                android:toDegrees="0">
                <bitmap android:src="@drawable/picture">
                </bitmap>
            </rotate>
        </item>
    </layer-list>
</item>
</layer-list>

console.log("you have selected" + "" title); +之间缺少"",这就是运行代码时导致错误的原因。

其次,您在最后一个超链接的结束title之前缺少<

即使在修复了这些内容之后,主要问题是您的函数设置为在加载HTML后立即运行。您需要为单击链接设置事件处理程序。

您也没有正确选择a元素的title属性。

请记住,虽然点击某个链接会导致用户离开当前页面,但是尝试将消息发送到控制台,只会在这些消息消失并加载新页面之前显示它们。当然,出于测试目的,您可以取消导航(正如我在下面的代码中所做的那样),这样您就不会实际导航并且能够看到该消息。

顺便说一句,键入a的快捷方式是jquery

$
// When all the HTML elements have been parsed into memory...
$(function() {
    // Get all the <a> elements that have a title attribute into a JQuery collection
    var $links = $("a[title]");
    
    // Give each member of the collection a click event handler
    $links.on("click", function(event){
       // Since clicking a link will navigate you away from this page,
       // we can stop that navigation (for testing purposes only) by
       // telling the event that is passed automatically to the function
       // to not continue
       event.preventDefault();
       
       // Then, we can access the specific element that triggered the event
       // via the "this" keyword and get its attribute value:
       console.log("you have selected " + $(this).attr('title'));
    });
});

答案 1 :(得分:0)

根据问题标题: (每次用户使用javascript 获取属性的值点击链接)

您的方法存在一些问题,让我们分析一下

jquery(function() {
    var title = jquery(title).attr('title');    
    console.log("you have selected" + "" title);
});

缺少+运算符来连接两个字符串。

console.log("you have selected" + "" title);
                                    ^

您使用了错误的jquery功能,请改用jQuery(Javascript区分大小写)。

jquery(function() {
^   var title = jquery(title).attr('title');    
                ^
    console.log("you have selected" + "" title);
});

包含最后一个a元素的标记错误。

<a title = "Social Media" href="https://www.facebook.com">Visit facebook/a>
                                                                        ^

请查看此代码段(仅限Javascript)

此方法适用于不允许使用jQuery等其他第三方框架的情况

&#13;
&#13;
// This is the IIFE (Immediately Invoked Function Expression) to bind the clik event to your links

function bindClickEvent() {
  document.querySelectorAll('a').forEach(function(a) {
    a.addEventListener('click', function(e) {
      e.preventDefault(); // This is to disable the default behavior of your links.

      var title = a.getAttribute('title');
      console.log("you have selected: " + title);
    });
  });
}

// Add this at the end of your body tag.
(function() {
  bindClickEvent();
})();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a title="google 1" href="https://www.google.com">Visit google</a>
<a title="Yahoo" href="https://www.yahoo.com">Visit This link</a>
<a title="Verizon Wireless" href="https://www.Verizon.com">Visit Verizon</a>
<a title="Social Media" href="https://www.facebook.com">Visit facebook</a>
&#13;
&#13;
&#13;

请参阅?现在您的链接与点击事件绑定。

资源