attr

How do I set an attr_accessor for a dynamic instance variable?

可紊 提交于 2019-12-18 10:24:39
问题 I dynamically created an instance variable within my class: class Mine attr_accessor :some_var def intialize @some_var = true end def my_number num self.instance_variable_set "@my_#{num}", num end end How do I make @my_#{num} now as an attr value? e.g. I want to be able to do this: dude = Mine.new dude.my_number 1 dude.my_1 => 1 回答1: this answer doesn't pollutes the class space, example.. if i do mine.my_number 4 then the other instances of Mine will not get the my_4 method.. this happens

Read nose tests arguments in a file especially @attr

ⅰ亾dé卋堺 提交于 2019-12-18 07:08:35
问题 If I invoke a test script say nosetests -a tag1='one' is there a way to print the user input of tag1 in my script? @attr(tag1=['one', 'two', 'three', 'four']) def test_real_logic(self): #how to print the user input here 回答1: Not without some pain. self.test_real_logic.tag1 should give you all the attributes attached to the function. They are stored as a dictionary within __dict__ attribute of the test function. For test_real_logic.tag1 it would be ['one', 'two', 'three', 'four']. If you do

JS .checked vs jquery attr('checked'), what is the difference?

杀马特。学长 韩版系。学妹 提交于 2019-12-18 05:49:07
问题 I can't figure this one out. According to W3 Schools, the checked property sets or returns the checked state of a checkbox. So why does $('input').checked ? $('div').slideDown() : $('div').slideUp(); not work? Using prop however, does work. $('input').prop('checked') ? $('div').slideDown() : $('div').slideUp(); This is for a checkbox that is checked based on a database value. 回答1: checked is a DOM element property so use it on DOM elements instead of jQuery objects. $('input')[0].checked if

get href attribute of enclosing link tag

安稳与你 提交于 2019-12-18 05:12:26
问题 Having a little trouble on this one. I need a way using Jquery/JS to find the HREF attribute of the enclosing link tag: <a href="something.html"><img src="img1.jpg" class="active"></a> I want to target the img by class and find the value of the 1st preceding href attribute. $("img.active").somethingAwesome().attr("href"); Please show me somethingAwesome() ...help? 回答1: $("img.active").parent("a").attr("href") will get the direct parent's href attribute, assuming it's an anchor. If there's any

jQuery .prop() returns undefined, while .attr() works as expected for data-*

空扰寡人 提交于 2019-12-18 03:39:59
问题 I am simply trying to get a couple of properties from two elements. Getting the property value from the input element works as expected. The problem is with getting the property data-detail property from the button element. It returns undefined when using .prop() , but works as expected when using .attr() . Can anyone explain this odd behaviour I am witnessing? HTML <div class="formRow"> <label for="firstName">First name</label> <div class="detailsControlBtns"> <button id="editFirstName"

jquery: get value of custom attribute

我只是一个虾纸丫 提交于 2019-12-18 03:00:57
问题 html5 supports the placeholder attribute on input[type=text] elements, but I need to handle non-compliant browsers. I know there are a thousand plugins out there for placeholder but I'd like to create the 1001st. I am able to get a handle on the input[placeholder] element but trying to get the value of the placeholder attribute is returning undefined - $("input[placeholder]").attr("placeholder") . I'm using jquery 1.6.2. Here is the jsfiddle. I modified the code to work in a browser that is

How to find elements with 'value=x'?

这一生的挚爱 提交于 2019-12-17 17:53:00
问题 I need to remove element that have value="123" . I know that all elements with different values are located into #attached_docs , but I don't know how to select element with value="123" . $('#attached_docs').find ... .remove(); Can you help me? 回答1: If the value is hardcoded in the source of the page using the value attribute then you can $('#attached_docs :input[value="123"]').remove(); If you want to target elements that have a value of 123 , which was set by the user or programmatically

Set attribute without value

筅森魡賤 提交于 2019-12-17 02:47:05
问题 How do I set a data attribute without adding a value in jQuery? I want this: <body data-body> I tried: $('body').attr('data-body'); // this is a getter, not working $('body').attr('data-body', null); // not adding anything Everything else seems to add the second arguments as a string. Is it possible to just set an attribute without value? 回答1: The attr() function is also a setter function. You can just pass it an empty string. $('body').attr('data-body',''); An empty string will simply create

difference between prop() and attr() in jQuery and when to use attr() and prop() [duplicate]

徘徊边缘 提交于 2019-12-17 02:10:30
问题 This question already has answers here : .prop() vs .attr() (17 answers) Closed 6 years ago . I saw in some places .attr() is used in jQuery.In some places .prop() is used.But i searched in SO and google i am very confused .Please tell me the exact difference between these two and when to use them. I have seen the following links jQuery attr vs. prop, there are a list of props? jQuery attr vs prop? But I did not got the answer.Please help me.Thanks in advance. Before giving a downvote please

Using jQuery .attr or .prop to set attribute value not working

一世执手 提交于 2019-12-14 03:41:59
问题 I've created a button with attribute named 'loaded' and initial value of 'no'. Upon clicking the button I'm running some ajax and at the very end of it I'm trying to set the 'loaded' attribute to 'yes' so that the ajax is not ran again if the user clicks on the button more than once. I have something like this: http://jsfiddle.net/PDW35/2/ Clicking the button does not change loaded to 'yes'. However, if you do an alert right after the .attr call like this: alert($(this).attr('loaded')); the