简体   繁体   English

使用onclick调用的功能切换文本

[英]Toggle text with function called with onclick

my question is a short one but I couldn't figure it out by myself. 我的问题很简短,但我自己无法解决。 I've got a function like -> http://jsfiddle.net/gtU56/ . 我有一个类似-> http://jsfiddle.net/gtU56/的函数。 I'm calling a javascript-function with an event-listener(onclick) in my html-code. 我正在用我的html代码中的事件监听器(onclick)调用javascript函数。 The function itself is more complex but I need the last snippet. 函数本身更复杂,但是我需要最后一个代码片段。 By clicking 'Show More' the text should change. 通过单击“显示更多”,文字应会更改。 Why won't the text change? 为什么文字不更改?

Because the toggleText function isn't available when the html code is rendered. 因为呈现html代码时toggleText函数不可用。

In other words the function isn't set until the page is ready so the onclick function doesn't reference anything. 换句话说,该功能直到页面准备好才设置,因此onclick函数不会引用任何内容。

Either have the function like here http://jsfiddle.net/gtU56/2/ 都有像这里的功能http://jsfiddle.net/gtU56/2/

or have it in the head of the page because its needs to be ready immediately 或将其放在页面顶部,因为它需要立即准备好

If you want it to wait for the ready state you can do the following and remove the onclick action all together 如果希望它等待ready状态,则可以执行以下操作并一起删除onclick操作

http://jsfiddle.net/gtU56/7/ http://jsfiddle.net/gtU56/7/

$(".text").click(function()
{                     
   $(".text").toggle();
});
toggleText = function () {
    $('.text').toggle();
}

check here http://jsfiddle.net/gtU56/3/ 在这里检查http://jsfiddle.net/gtU56/3/

It's because of how you're loading the function. 这是因为您如何加载函数。 Switch it from onLoad to no wrap (head) and it works fine. 将其从onLoad切换为no wrap(头部),即可正常工作。

jsFiddle example jsFiddle示例

Using jsFiddle's onLoad wraps your function in a window.onload call like this: 使用jsFiddle的onLoad将函数包装在window.onload调用中,如下所示:

<script type="text/javascript">
//<![CDATA[
$(window).load(function(){
function toggleText() {
$('.text').toggle();
}
});//]]> 
</script>

While no wrap (head) just adds it normally like this: 虽然没有换行(头)通常只是这样添加它:

<script type="text/javascript">
//<![CDATA[
function toggleText() {
$('.text').toggle();
}
//]]> 
</script>

Is very easy. 很简单。 You can use ID or CLASS. 您可以使用ID或CLASS。

onclick="$('NAME ID or CLASS').toggle(ANIMATION or DURATION);"


<div>
    <div class="text" onclick="$('.text2').toggle(400);">Show More</div>
    <div class="text2" style="display:none">Show Less</div>
</div>

since you are already claiming having jquery , you need not use inline javascript. 由于您已经声称拥有jquery ,因此无需使用嵌入式javascript。 try this 尝试这个

var elems = $('.text');
elems.click(function(){
  elems.toggle(); 
});

fiddle : http://jsfiddle.net/gtU56/5/ 小提琴: http : //jsfiddle.net/gtU56/5/

$('.text').click(function() {
  $('.text').toggle('slow', function() {
    // do your animation..
  });
});

Js Fiddle 爵士小提琴

This is the solution - http://jsfiddle.net/gtU56/6/ 这是解决方案-http://jsfiddle.net/gtU56/6/

You need to first make sure that the function is registered after page load. 您首先需要确保在页面加载后注册了该功能。 Then, bind a click event to the div. 然后,将click事件绑定到div。

Hope this helps! 希望这可以帮助!

First you should organize you jQuery code like this: 首先,您应该像这样组织jQuery代码:

$.(document).ready(function() {
// enter jQuery code here
});

Otherwise you're accessing a not completly loaded html document. 否则,您将访问未完全加载的html文档。

Also, you don't need the event-listener if you are using jQuery. 另外,如果您使用的是jQuery,则不需要事件监听器。

This should work for you: 这应该为您工作:

$.(document).ready(function() {
   $('.text').click(function() {
      $(this).toggle();
   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM