简体   繁体   English

将jQuery样式应用于动态创建的按钮

[英]Applying jquery style to dynamically created buttons

I am new to uI programming. 我是uI编程的新手。 I am using Jqgrid in my application. 我在我的应用程序中使用Jqgrid。 I am having one column which displays button in each row , created using formatter option. 我有一列在每一行中显示按钮,使用formatter选项创建。 In function described in formatter option I am binding a click event for the button. 在格式化程序选项中描述的功能中,我绑定了按钮的click事件。 Below is the code 下面是代码

        { label: 'Depatment Name', name: 'deptName', width: 100,
              formatter:actionButtonFormatter
        },

      function actionButtonFormatter ( cellvalue, options, rowObject )
       {
          var element ='<div id="deptNmBtn"><button onClick=getDepartMentNm("' + rowObject.empName+'")> <span>Add</span></button></div>';
          $("button, input:submit, input:button", this).button();
          return element;
       }

I am facing two problems here. 我在这里面临两个问题。

1) Jquery theme is not getting applied to the button , in Department Name column which was added using formatter option. 1)jQuery主题未应用到使用formatter选项添加的Department Name列中的按钮。 Where I want only this button to get applied with Jquery theme so I tried 我只想将此按钮与Jquery主题一起应用,所以我尝试了

         $("button, input:submit, input:button", this).button();
         $("button, input:submit, input:button", jqgrid_table_id).button();  
         $("button, input:submit, input:button", deptNmBtn).button(); //div id

But none of them worked and the button in the column is not getting applied with Jquery theme. 但是它们都不起作用,并且该列中的按钮未应用Jquery主题。

2) In onclick I am passing a parameter of employeeName which is value of the row. 2)在onclick中,我传递了employeeName的参数,该参数是该行的值。 It works well if the employee name doesn't contain any spaces. 如果员工姓名不包含任何空格,则此方法效果很好。 If the employee name contains spaces it throws me 如果员工姓名中包含空格,则会抛出我

        SyntaxError: unterminated string literal

taken from firebug. 取自萤火虫。 Have anyone faced these issues. 有没有人面对这些问题。 Please help me out. 请帮帮我。 And also please help in understaing the difference between 并且还请帮助保持两者之间的差异

        var element = '<div> ....'

and

        var element = $('<div> ....')

I know the second one is jquery syntax but whats the exact difference. 我知道第二个是jquery语法,但是确切的区别是什么。 Thanks a lot. 非常感谢。

Add these classes 添加这些类

ui-button ui-widget ui-state-default ui-corner-all

On hover add ui-state-highlight . 在悬停时添加ui-state-highlight

Not sure I can cover all the bases here... 不确定我是否可以涵盖这里的所有基础...

1) I don't recall the jQuery UI method for adding the styles. 1)我不记得添加样式的jQuery UI方法。 But I never do this anyhow. 但是我从不这样做。 Find out which classes need to be a part of your element, and add them during the button creation. 找出哪些类需要成为元素的一部分,并在创建按钮期间添加它们。 So instead of making a button and then calling .button() on it, you would make it something as "<button class="someclass otherclass">Button</button>" where the classes are filled with those needed for the jQuery theme. 因此,与其制作一个按钮然后在其上调用.button(),不如将其"<button class="someclass otherclass">Button</button>""<button class="someclass otherclass">Button</button>" ,在"<button class="someclass otherclass">Button</button>"中填充了jQuery主题所需的类。 。

2) You probably shouldn't be using onclick inline anyhow, but that's another discussion. 2)无论如何,您可能不应该使用onclick内联,但这是另一个讨论。 In the meantime, it's quite simple: the first one is a string, which could eventually end up being used by some JavaScript method (in jQuery, something like .append() ) to attach a new node to the DOM. 同时,这很简单:第一个是字符串,最终可能会被某些JavaScript方法(在jQuery中,如.append() )使用,以将新节点附加到DOM。 The second one creates the node object, but it still needs to be attached to the DOM with... something like .append() . 第二个创建节点对象,但仍然需要使用.append()类的东西将其附加到DOM。 ;-) Under the hood, there's a difference. ;-)引擎盖下有一个区别。 But for most purposes it doesn't matter TOO much. 但是对于大多数目的来说,这并没有太大关系。

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

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