use*_*363 2 ruby-on-rails ruby-on-rails-3.1
以下link_to_function有效:
<%= link_to_function "click", "alert(Hallo world!" %>
Run Code Online (Sandbox Code Playgroud)
但是,使用jquery,以下方法不起作用:
<%= link_to_function "click", "$('#std').append('<p>my friend!</p>')" %>
Run Code Online (Sandbox Code Playgroud)
在application.html.erb中,有:
<head>
<title><%= title %></title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
Run Code Online (Sandbox Code Playgroud)
jquery似乎运作良好.有关这个问题的任何想法?谢谢.
另外这里是rails 3.1.3中关于link_to_function的api定义:
link_to_function(name, function, html_options={})
Returns a link whose onclick handler triggers the passed JavaScript.
The helper receives a name, JavaScript code, and an optional hash of HTML options. The name is used as the link text and the JavaScript code goes into the onclick attribute. If html_options has an :onclick, that one is put before function. Once all the JavaScript is set, the helper appends “; return false;”.
The href attribute of the tag is set to “#” unless html_options has one.
link_to_function "Greeting", "alert('Hello world!')", :class => "nav_link"
# => <a class="nav_link" href="#" onclick="alert('Hello world!'); return false;">Greeting</a>
Run Code Online (Sandbox Code Playgroud)
更新:这是html酸代码:
<a href="#" onclick="$('#std').append('<p>my friend!</p>'); return false;">test</a>
Run Code Online (Sandbox Code Playgroud)
这是编写javascript的突兀方式.我会写这样的东西:
<%= link_to "click", "#", :id=>"click" %>
Run Code Online (Sandbox Code Playgroud)
更新
您应该添加文档准备好具有正确的绑定.
然后在你的application.js中
$(function() {
$("#click").click(function(e){
$('#std').append('<p>my friend!</p>');
e.preventDefault();
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2070 次 |
| 最近记录: |