从以前的td获取文本(Get text from previous td)
我有一个按钮,当单击时,动态构建一个简单的表。 在第二个
td
,插入一个按钮以及一些文本。<input type = "button" id = "search" value = "Search"> <div id = "searcharea"></div>
$('#search').on('click', function() { $('#searcharea').html('<table id = "myTable"><tr><td>Some Text</td><td>Blah Blah<br>Blah Blah<br><input type = "button" value = "Get Text"></td></tr></table>'); });
我要做的是在点击“获取文本”按钮时从前一个
td
获取文本。 这是我尝试过的:$('#myTable').on('click', 'input', function (event) { var prevCell = $(this).closest('td').previous('td').text(); alert(preCell); });
我认为答案很简单,但我没有看到。 救命。 的jsfiddle
I have a button that, when clicked, builds a simple table dynamically. In the second
td
, a button is inserted along with some text.<input type = "button" id = "search" value = "Search"> <div id = "searcharea"></div>
$('#search').on('click', function() { $('#searcharea').html('<table id = "myTable"><tr><td>Some Text</td><td>Blah Blah<br>Blah Blah<br><input type = "button" value = "Get Text"></td></tr></table>'); });
What I'm trying to do is to get the text from the previous
td
when the "Get Text" button is clicked. Here's what I've tried:$('#myTable').on('click', 'input', function (event) { var prevCell = $(this).closest('td').previous('td').text(); alert(preCell); });
I think the answer is a simple one, but I'm not seeing it. Help. JSFIDDLE
原文:
最满意答案
我认为这是一个很好的问题! 因为好的做法存在“矛盾”。
第一个解决方案应用规则:“尝试将最少数量的参数传递给方法(或构造函数)”(Robert C martin在他的书“清洁代码”中提到了这条规则)
第二个片段遵循得墨忒耳的法则,该法则规定 MyClass应该尽可能少地了解A.
我个人会选择第二种选择,但在某些情况下我可能会选择第一种选择。
I think it's an excellent question! as there's a "contradiction" of good practices.
The first solution applies the rule: "try to pass the least number of parameters to a method (or constructor)" (Robert C martin mentions this rule in his book Clean Code)
The second snippet follows the rule of Law of Demeter, which says That MyClass should know as little as possible about A.
Personally I would go with the second option, but in some scenarios I might go for the first one.
相关问答
更多-
如何将无参数构造函数的对象置入std :: map?(How to emplace object with no-argument constructor into std::map?)[2021-10-09]
std::map的元素类型实际上是std::pair ,所以当你放置到一个映射中时,参数将被转发给std::pair的构造函数。 这就是为什么你不能只传递键: std::pair 不能从一个参数构造(除非它是另一个相同类型的对)。你可以传递零参数,但是然后键将被初始化,这可能不是你想要的。 在大多数情况下,移动值将会很便宜(并且密钥会很小并且可以复制),您应该只是这样做: M.emplace(k, V{}); 其中V是映射类型。 它将被初始化并移入容器。 (这个举动甚至可 ... -
我猜测“但它不起作用”你的意思是,你想将每个数据包添加到数组的单独条目中。 您可以遍历数组并一次添加一个数据包。 public void enqueue(Packet...packets) { for(Packet packet : packets){ queue.add(packet); } } 注意: Packet... packets (或多或少)是另一种说Packet[] packets I'm guessing that "but it doesn't work" you me ...
-
#include
... std::vector s(n, set(x,y,z)); 这将创建n个set对象的向量(动态可调整大小的数组),每个对象都是set(x,y,z)的副本。 如果要对各种元素使用不同的构造函数,或者使用具有不同参数的相同构造函数: std::vector s; // create empty vector s.push_back(set(x,y,z)); s.push_back(set(y,z,x)); ... ... // repe ... -
许多add_argument参数都是关键字,可以通过**kwargs语法传递。 例如: In [90]: parser=argparse.ArgumentParser() In [91]: adict={"metavar":"bar", "help":"help message", "default":"default"} In [92]: a1 = parser.add_argument('--foo', **adict) In [93]: a1 Out[93]: _StoreAction(option_ ...
-
构造函数中的React绑定方法 - 如何传入Argument(React binding method in constructor - how to pass in Argument)[2022-03-25]
不要担心构造函数中的bind 。 相反,您可以在onCheck声明中绑定它,并将referralSource参数传递给bind 。 onCheck={this.handleChangeProductType.bind(null, referralSource)} Don't worry about the bind in the constructor. Instead, you can bind it in the onCheck declaration and pass in the referral ... -
只需为它创建一个字段,就像你为数组所做的那样。 public class MyClass{ private int[][] myArray; private int myArraySize; public MyClass(int N){ if(N <= 0) throw new IndexOutOfBoundsException("Input Error: N <= 0"); myArray = new int[N][N]; ...
-
如果使用self.date = datetime.date(*date)指定日期,则可以通过传递(年,月,日)元组来创建Work_day : day = Work_day((2013,5,31), 8.0, 8.25) 或者,如果您希望输入是日期字符串,请使用datetime.strptime (一个适当的格式化字符串)和date()方法来获取date对象: self.date = datetime.datetime.strptime(date,'%m/%d/%Y').date() ... date = ...
-
您可以使用非零参数构造函数创建对象,并在单元测试与类位于同一包中时对受保护方法进行单元测试。 如果绝对有必要压制构造函数, PowerMock可以做到这一点。 I already found a solution. I created a test class: public class testAbc extends abc { public testAbc() {super("x","y");} @Override public Object fun(String a){ Object x = supe ...
-
我认为这是一个很好的问题! 因为好的做法存在“矛盾”。 第一个解决方案应用规则:“尝试将最少数量的参数传递给方法(或构造函数)”(Robert C martin在他的书“清洁代码”中提到了这条规则) 第二个片段遵循得墨忒耳的法则,该法则规定 MyClass应该尽可能少地了解A. 我个人会选择第二种选择,但在某些情况下我可能会选择第一种选择。 I think it's an excellent question! as there's a "contradiction" of good practices. ...
-
对于名为add的按钮对象的方法,下面给出的参数是什么意思(what is meant by argument as given below for method of button object named add)[2022-02-18]
该参数是一个匿名类型子类的新匿名对象,它从View.OnClickListener中添加到名为add的按钮中。 该定义引用此代码给出的new View.OnClickListener() { /*...*/ }参数new View.OnClickListener() { /*...*/ } 。 这就是你的行中发生的事情。 每当按下按钮add将在传入的匿名对象上调用onClick方法。然后执行覆盖onClick方法内的代码。 您可以通过将匿名类分成如下所示的命名类来使其更清晰: class MyButtonL ...