我使用Ajax从数据库中获取一些数据,从该Ajax请求返回的数据:
<p class="fetchedData">First Result</p>
<p class="fetchedData">Second Result</p>
<p class="fetchedData">Third Result</p>
<p class="fetchedData">Fourth Result</p>
如何在点击每个p
元素时获取文本?
比如点击<p class="fetchedData">Fourth Result</p>
,我得到“第四个结果”。
我试过
var eachData $('.fetchedData').each(function(){
$(this).on('click' , function(){
console.log( $(this).text() );
})
});
并且
var allData = document.querySelectorAll('#fetchedData');
allData.forEach(function(el){
console.log( $(this).text() );
}
但他们都没有工作。
我也试过
$(document).on('click','.fetchedData' , function () {
console.log( $(this).text() );
})
但它只适用于第一个元素。
答案 0 :(得分:2)
smap
&#13;
private void handleActionDelete(long event, long occurrence) {
final ContentResolver contentResolver = getContentResolver();
Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(eventsUriBuilder, Long.MIN_VALUE);
ContentUris.appendId(eventsUriBuilder, Long.MAX_VALUE);
Uri eventsUri = eventsUriBuilder.build();
Cursor cursor;
String[] projection;
projection = new String[]{CalendarContract.Instances.BEGIN};
String selection = "Instances." + CalendarContract.Instances._ID + " = ? AND " + CalendarContract.Instances
.EVENT_ID + " = ?";
String[] selArgs = new String[]{Long.toString(occurrence), Long.toString(event)};
cursor = getContentResolver().query(eventsUri, projection, selection, selArgs, null);
if (cursor == null) {
return;
}
if (cursor.getCount() == 0) {
cursor.close();
return;
}
cursor.moveToFirst();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.ORIGINAL_INSTANCE_TIME, cursor.getLong(cursor.getColumnIndex
(CalendarContract.Instances.BEGIN)));
values.put(CalendarContract.Events.STATUS, CalendarContract.Events.STATUS_CANCELED);
Uri uri = Uri.withAppendedPath(CalendarContract.Events.CONTENT_EXCEPTION_URI, String.valueOf(event));
cursor.close();
try {
contentResolver.insert(uri, values);
} catch (Exception ignored) {
}
}
&#13;
答案 1 :(得分:0)
您需要删除var eachData,并且只需使用jQuery添加点击事件
$('.fetchedData').each(function(){
$(this).on('click' , function(){
console.log( $(this).text() );
})
});
答案 2 :(得分:0)
使用.html()点击上面的链接以供参考
或者使用document.getElementByClassName("className").value
答案 3 :(得分:0)
如果每个数据都有相同的类,则无需使用每个循环。您可以直接在.fetchedData类上单击事件。
Purpose.findOne({name : 'Newsfeed'}).exec(function(err, result){
if(err) console.log(err)
});