我需要从html表中获取userid,data-attribute的值并将此值放入var中,但我想在没有单击操作的情况下执行此操作。
<table id="tblList">
<tbody id="someTest">
<tr data-userid="801992084067">
<tr data-userid="451207954179">
<tr data-userid="310896831399">
<tr data-userid="863939754980">
<tr data-userid="1123542226482">
</tbody>
</table>
我试过这样做,但是rowId是未定义的。
var rowId = $("#someTest tr").last().attr("[data-userid"]");
答案 0 :(得分:3)
简单地说,您可以管理数据属性&amp;使用jQuery的data()
方法在HTML标记中的值。或者,您也可以使用attr()方法,
var rowId = $("#someTest tr").last().data("userid");
可选地
var rowId = $("#someTest tr").last().attr("data-userid");
.data()方法用于存储与匹配元素关联的任意数据或返回 指定数据存储区中第一个元素的值 匹配的元素。
初始HTML
<button id="mybtn">MyButton</button>
添加值为的数据属性
$('button#mybtn').data('id',10);
可选地
$('button#mybtn').data('data-id',10);
转载HTML
<button id="mybtn" data-id="10">MyButton</button>
从数据属性中获取值
alert($('button#mybtn').data('id')); //alerts 10
可选地
alert($('button#mybtn').attr('data-id')); //alerts 10
更改数据属性的值
$('button#mybtn').data('id',15);
可选地
$('button#mybtn').attr('data-id',15);
转载HTML
<button id="mybtn" data-id="15">MyButton</button>
删除数据属性
您可以使用removeData()
方法
$('button#mybtn').removeData('id');
可选地
$('button#mybtn').removeAttr('data-id');
转载HTML
<button id="mybtn">MyButton</button>
答案 1 :(得分:2)
仅删除webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
:
09-02 14:22:05.258 19661-19661/com.example.obs I/Timeline: Timeline: Activity_launch_request id:com.example.obs time:12875631 beginning of system
09-02 14:22:05.311 19661-19675/com.example.obs W/art: Suspending all threads took: 13.479ms 09-02 14:22:05.396 19661-19661/com.example.obs I/WebViewFactory: Loading com.google.android.webview version 51.0.2704.81 (code 270408150)
09-02 14:22:05.409 19661-19661/com.example.obs I/cr_LibraryLoader: Time to load native libraries: 1 ms (timestamps 5781-5782)
09-02 14:22:05.409 19661-19661/com.example.obs I/cr_LibraryLoader: Expected native library version number "51.0.2704.81", actual native library version number "51.0.2704.81"
09-02 14:22:05.416 19661-19661/com.example.obs V/WebViewChromiumFactoryProvider: Binding Chromium to main looper Looper (main, tid 1) {21b37efb}
09-02 14:22:05.416 19661-19661/com.example.obs I/cr_LibraryLoader: Expected native library version number "51.0.2704.81", actual native library version number "51.0.2704.81"
09-02 14:22:05.416 19661-19661/com.example.obs I/chromium: [INFO:library_loader_hooks.cc(143)] Chromium logging enabled: level = 0, default verbosity = 0
09-02 14:22:05.438 19661-19661/com.example.obs I/cr_BrowserStartup: Initializing chromium process, singleProcess=true
09-02 14:22:05.488 19661-20394/com.example.obs W/cr_media: Requires BLUETOOTH permission
09-02 14:22:05.503 19661-19661/com.example.obs I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.WebViewContentsClientAdapter$WebResourceErrorImpl>
09-02 14:22:05.503 19661-19661/com.example.obs I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.WebViewContentsClientAdapter$WebResourceErrorImpl>
09-02 14:22:05.528 19661-20396/com.example.obs D/ConnectivityManager.CallbackHandler: CM callback handler got msg 524290
09-02 14:22:05.547 19661-19661/com.example.obs I/art: Rejecting re-init on previously-failed class java.lang.Class<org.chromium.content.browser.FloatingWebActionModeCallback>
09-02 14:22:05.547 19661-19661/com.example.obs I/art: Rejecting re-init on previously-failed class java.lang.Class<org.chromium.content.browser.FloatingWebActionModeCallback>
09-02 14:22:05.561 19661-19661/com.example.obs D/cr_Ime: [InputMethodManagerWrapper.java:30] Constructor
09-02 14:22:05.569 19661-19661/com.example.obs W/cr_AwContents: onDetachedFromWindow called when already detached. Ignoring
09-02 14:22:05.570 19661-19661/com.example.obs D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: false
09-02 14:22:05.611 19661-19661/com.example.obs I/cr_Ime: ImeThread is not enabled.
09-02 14:22:05.637 19661-20403/com.example.obs E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
09-02 14:22:05.656 19661-20403/com.example.obs W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
09-02 14:22:05.663 19661-20403/com.example.obs W/AudioCapabilities: Unsupported mime audio/qcelp
09-02 14:22:05.664 19661-20403/com.example.obs W/AudioCapabilities: Unsupported mime audio/evrc
09-02 14:22:05.669 19661-20403/com.example.obs W/VideoCapabilities: Unsupported mime video/mpeg2
09-02 14:22:05.670 19661-20403/com.example.obs W/VideoCapabilities: Unsupported mime video/mpeg2
09-02 14:22:05.675 19661-20403/com.example.obs W/VideoCapabilities: Unsupported mime video/x-ms-wmv
09-02 14:22:05.677 19661-20403/com.example.obs W/VideoCapabilities: Unsupported mime video/divx
09-02 14:22:05.680 19661-20403/com.example.obs W/VideoCapabilities: Unsupported mime video/divx311
09-02 14:22:05.680 19661-20403/com.example.obs W/Utils: could not parse size range '64x64-1920X1088'
09-02 14:22:05.683 19661-20403/com.example.obs W/VideoCapabilities: Unsupported mime video/divx4
09-02 14:22:05.688 19661-20403/com.example.obs W/Utils: could not parse size range '64x64-1920X1080'
09-02 14:22:05.691 19661-20403/com.example.obs W/VideoCapabilities: Unsupported mime video/mp4v-esdp
09-02 14:22:05.704 19661-20403/com.example.obs I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es
09-02 14:22:05.708 19661-20403/com.example.obs W/AudioCapabilities: Unsupported mime audio/ac3
09-02 14:22:05.710 19661-20403/com.example.obs W/AudioCapabilities: Unsupported mime audio/x-ape
09-02 14:22:05.711 19661-20403/com.example.obs W/AudioCapabilities: Unsupported mime audio/ffmpeg
09-02 14:22:05.712 19661-20403/com.example.obs W/AudioCapabilities: Unsupported mime audio/vnd.dts
09-02 14:22:05.714 19661-20403/com.example.obs W/AudioCapabilities: Unsupported mime audio/mpeg-L2
09-02 14:22:05.716 19661-20403/com.example.obs W/AudioCapabilities: Unsupported mime audio/vnd.rn-realaudio
09-02 14:22:05.717 19661-20403/com.example.obs W/AudioCapabilities: Unsupported mime audio/x-ms-wma
09-02 14:22:05.718 19661-20403/com.example.obs W/VideoCapabilities: Unsupported mime video/divx
09-02 14:22:05.720 19661-20403/com.example.obs W/VideoCapabilities: Unsupported mime video/x-flv
09-02 14:22:05.722 19661-20403/com.example.obs W/VideoCapabilities: Unsupported mime video/mpeg2
09-02 14:22:05.726 19661-20403/com.example.obs W/VideoCapabilities: Unsupported mime video/vnd.rn-realvideo
09-02 14:22:05.728 19661-20403/com.example.obs W/VideoCapabilities: Unsupported mime video/vc1
09-02 14:22:05.730 19661-20403/com.example.obs W/VideoCapabilities: Unsupported mime video/ffmpeg
09-02 14:22:05.732 19661-20403/com.example.obs W/VideoCapabilities: Unsupported mime video/x-ms-wmv
09-02 14:22:05.736 19661-20396/com.example.obs D/ConnectivityManager.CallbackHandler: CM callback handler got msg 524290
09-02 14:22:05.863 19661-19795/com.example.obs V/RenderScript: Application requested CPU execution
09-02 14:22:05.866 19661-19795/com.example.obs V/RenderScript: 0x5586157780 Launching thread(s), CPUs 8
09-02 14:22:05.889 19661-19661/com.example.obs W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 19661
09-02 14:22:05.890 19661-19661/com.example.obs D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: false
09-02 14:22:05.894 19661-19661/com.example.obs I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@4a1d642 time:12876268
最终代码:
[]
答案 2 :(得分:1)
您只需要删除方括号:
var rowId = $("#someTest tr").last().attr("data-userid");
$('#rowidOutputAttr').text(rowId);
var rowId = $("#someTest tr").last().data("userid");
$('#rowidOutputData').text(rowId);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<body>
<table id="tblList">
<tbody id="someTest">
<tr data-userid="801992084067">
<tr data-userid="451207954179">
<tr data-userid="310896831399">
<tr data-userid="863939754980">
<tr data-userid="1123542226482">
</tbody>
</table>
<div id=rowidOutputAttr></div>
<div id=rowidOutputData></div>
</body>
</html>
我还在.data()