在我的锚标记的敲除点击绑定功能之前,首先执行javascript点击

时间:2014-03-16 07:42:44

标签: javascript jquery knockout.js onclick anchor

我正在处理单页应用程序,并且在我的某个页面中有锚标记。当点击任何锚标签时,我写了我的逻辑来执行。它的形式是

$( "a" ).on( "click", function( e ) {

    // My LOGIC CODE  Here to change Window's Location.

  });

我所有类型的链接都被执行得很好(例如:a href =" / page1") 除了一个使用敲除绑定的锚标签。这个锚标签是形式的 (a href =" / page1" data-bind =" click:myFunction")

我的问题是通过单击该锚标记首先调用MyFunction()然后它正在执行事件我的事件处理程序,因此无法正常工作。

相反,我想首先执行我的事件处理程序,然后执行函数(Knockout&#39点击绑定)。

请提供解决方案。谢谢。

2 个答案:

答案 0 :(得分:0)

使用来源,卢克。 ;)

在创建事件处理程序的函数中(如click绑定),there is this part

if (handlerReturnValue !== true) {
    // Normally we want to prevent default action. 
    // Developer can override this be explicitly returning true.
    if (event.preventDefault)
        event.preventDefault();
    else
        event.returnValue = false;
}

只需从true返回myFunction

答案 1 :(得分:0)

或者,您可以在数据绑定上返回true,如下所示:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace MyExample
{
    class Program
    {
        static void Main(string[] args)
        {
            IncredibleDataObject me = new IncredibleDataObject();
            IIncredibleDataObject customer = (IIncredibleDataObject)me;
            // This is me
            JustSomeData data = new JustSomeData();
            data.Elbow = 2;
            me.DataCollection.Add(new JustSomeData());
            me.DataCollection.Add(data);

            // Customer
            double shouldBeTwo = customer.IDataCollection[1].Elbow;
            customer.IDataCollection[1].Toes = 10;

            // Me 
            double shouldBeTen = me.DataCollection[1].Toes;

            // Need to be able to do this
            TextWriter writer = new StreamWriter(@"C:\test.xml");
            XmlSerializer serializer = new XmlSerializer(typeof(IncredibleDataObject));
            // Use the Deserialize method to restore the object's state.
            serializer.Serialize(writer, me);
            writer.Close();

            IncredibleDataObject openData = new IncredibleDataObject();
            TextReader reader = new StreamReader(@"C:\test.xml");
            XmlSerializer deserializer = new XmlSerializer(typeof(IncredibleDataObject));
            // Use the Deserialize method to restore the object's state.
            openData = (IncredibleDataObject)deserializer.Deserialize(reader);
            reader.Close();
        }
    }
}