jQuery click事件正在影响具有相同类的所有标记

时间:2017-02-08 02:39:14

标签: javascript jquery html click

我正在使用node-horseman,我正在使用.evaluate()和jQuery在Google快讯上的按钮上触发click功能。它有效,但它影响了所有.delete_button

每个li都有data-id属性,该属性具有唯一值。它是由Google快讯本身创建的,因此我利用此功能选择了node-horseman的正确数据块,并通过.html()测试,它运行良好,它确实按给定的{{选择了正确的数据块1}}。

data-id

基于给定jQuery('#manage-alerts-div li').attr('data-id', this.feedID).find('.alert_buttons').html() 的警报检查代码如下:

enter image description here

返回的带有jQuery选择的HTML:

data-id

然后,我尝试点击<a href="/alerts/feeds/07997923364799863317/3782479325533077987" tabindex="0"><span class="rss_icon" title="RSS"></span></a><span class="edit_button alert_button" title="Edit" role="button" tabindex="0"></span><span class="delete_button alert_button" title="Delete" role="button" tabindex="0"></span>

.delete_button

但它坚持影响所有现有的jQuery('#manage-alerts-div li').attr('data-id', this.feedID).find('.alert_buttons > .delete_button').trigger('click') 类。

1 个答案:

答案 0 :(得分:2)

如果您尝试仅选择具有特定属性值的元素,请尝试attribute equals selector,如下所示:

function varargout = Real2(varargin)

% REAL2 MATLAB code for Real2.fig

%      REAL2, by itself, creates a new REAL2 or raises the existing

%      singleton*.

%

%      H = REAL2 returns the handle to a new REAL2 or the handle to

%      the existing singleton*.

%

%      REAL2('CALLBACK',hObject,eventData,handles,...) calls the local

%      function named CALLBACK in REAL2.M with the given input arguments.

%

%      REAL2('Property','Value',...) creates a new REAL2 or raises the

%      existing singleton*.  Starting from the left, property value pairs are

%      applied to the GUI before Real2_OpeningFcn gets called.  An

%      unrecognized property name or invalid value makes property application

%      stop.  All inputs are passed to Real2_OpeningFcn via varargin.

%

%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one

%      instance to run (singleton)".

%

% See also: GUIDE, GUIDATA, GUIHANDLES




% Edit the above text to modify the response to help Real2




% Last Modified by GUIDE v2.5 07-Feb-2017 18:09:44




% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

gui_State = struct('gui_Name',       mfilename, ...

                   'gui_Singleton',  gui_Singleton, ...

                   'gui_OpeningFcn', @Real2_OpeningFcn, ...

                   'gui_OutputFcn',  @Real2_OutputFcn, ...

                   'gui_LayoutFcn',  [] , ...

                   'gui_Callback',   []);

if nargin && ischar(varargin{1})

    gui_State.gui_Callback = str2func(varargin{1});

end




if nargout

    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});

else

    gui_mainfcn(gui_State, varargin{:});

end

% End initialization code - DO NOT EDIT







% --- Executes just before Real2 is made visible.

function Real2_OpeningFcn(hObject, eventdata, handles, varargin)

% This function has no output args, see OutputFcn.

% hObject    handle to figure

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% varargin   command line arguments to Real2 (see VARARGIN)




% Choose default command line output for Real2

handles.output = hObject;




% Update handles structure

guidata(hObject, handles);

initialize_gui(hObject, handles, false);

% UIWAIT makes Real2 wait for user response (see UIRESUME)

% uiwait(handles.figure1);







% --- Outputs from this function are returned to the command line.

function varargout = Real2_OutputFcn(hObject, eventdata, handles) 

% Get default command line output from handles structure

varargout{1} = handles.output;










%-----------------------------------------------------------------------------------%













function Number1_Callback(hObject, eventdata, handles)

Number1 = str2double(get(hObject, 'String'));




handles.metricdata.Number1 = Number1;

guidata(hObject,handles)




% --- Executes during object creation, after setting all properties.

function Number1_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');

end










%-----------------------------------------------------------------------------------%







function Number2_Callback(hObject, eventdata, handles)

Number2 = str2double(get(hObject, 'String'));

handles.metricdata.Number2 = Number2;

guidata(hObject,handles)




% --- Executes during object creation, after setting all properties.

function Number2_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');

end










%-----------------------------------------------------------------------------------%













function Number3_Callback(hObject, eventdata, handles)

Number3 = str2double(get(hObject, 'String'));

handles.metricdata.Number3 = Number3;

guidata(hObject,handles)




% --- Executes during object creation, after setting all properties.

function Number3_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');

end



















function Number4_Callback(hObject, eventdata, handles)

Number4 = str2double(get(hObject, 'String'));

handles.metricdata.Number4 = Number4;

guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.

function Number4_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');

end













%-----------------------------------------------------------------------------------%

% --------------------------------------------------------------------

% --- Executes on button press in Togz.

function Togz_Callback(hObject, eventdata, handles)

% hObject    handle to Togz (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)




% Hint: get(hObject,'Value') returns toggle state of Togz

button_state = get(hObject,'Value');

handles.button_state = button_state;

set(handles.funcz, 'String', 0);

%-----------------------------------------------------------------------------------%




% --- Executes on button press in pushbutton1.

function pushbutton1_Callback(hObject, eventdata, handles)

% hObject    handle to pushbutton1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)




average = (handles.metricdata.Number1+handles.metricdata.Number2+handles.metricdata.Number3+handles.metricdata.Number4)/4;

funcz= 2*average^2-3*average+2;

set(handles.average, 'String', average);




if handles.button_state==true

    set(handles.funcz, 'String', 0);

else

    set(handles.funcz, 'String', funcz);

end










function initialize_gui(fig_handle, handles, isreset)

% If the metricdata field is present and the reset flag is false, it means

% we are we are just re-initializing a GUI by calling it from the cmd line

% while it is up. So, bail out as we dont want to reset the data.

if isfield(handles, 'metricdata') && ~isreset

    return;

end




handles.metricdata.Number1 = 0;

handles.metricdata.Number2 = 0;

handles.metricdata.Number3 = 0;

handles.metricdata.Number4 = 0;

handles.button_state = false;




set(handles.Number1, 'String', handles.metricdata.Number1);

set(handles.Number2, 'String', handles.metricdata.Number2);

set(handles.Number3, 'String', handles.metricdata.Number3);

set(handles.Number4, 'String', handles.metricdata.Number4);

set(handles.funcz, 'String', 1);

set(handles.average, 'String', 0);










% Update handles structure

guidata(handles.figure1, handles);

以下是您现有代码的详细信息:

jQuery('#manage-alerts-div li[data-id="' + this.feedID + '"]')
  .find('.alert_buttons > .delete_button')
  .trigger('click');

...选择jQuery('#manage-alerts-div li') 中的所有li元素,然后选择

#manager-alerts-div

... 将<{1}}属性设置为.attr('data-id', this.feedID) ,然后

data-id

...在所有this.feedID元素的原始集合中查找元素,然后

.find('.alert_buttons > .delete_button')

...触发所有这些的点击处理程序。

您提到的'#manage-alerts-div li'测试可能会让事情变得混乱,因为.trigger('click') 只返回jQuery对象中第一个元素的内容,即使您使用的选择器与许多元素匹配,所以可能会让你觉得你只选择了一个元素。