我不知道这是允许的,甚至是不可能的.是否可以使用onclick事件创建div元素,以便在单击div区域中的任何位置时,将运行该事件.
例如JAVASCRIPT:
var divTag = document.createElement("div");
divTag.id="new-ID";
var onClickCommand = "printWorking()" //printworking is another method that simply print "I'm working" to the console
divTag.onclick = onClickCommand;
console.log("onclick command added");
console.log(divTag.onclick);
parentDiv.appendChild(divTag); //This simply adds the div tag to the page.
Run Code Online (Sandbox Code Playgroud)
生成的HTML:
<!--<div id="new-ID">-->
whatever content I decide to add to the divTag.innerHTML
Run Code Online (Sandbox Code Playgroud)
我注意到如何没有onclick命令进入生成的div.这是因为它不允许吗?
那么2个问题呢.
1:是否可以向div添加onclick并在单击div的任何区域时发生.2:如果是,那么为什么onclick方法不会进入我的div.
多奇怪啊:
divTag.setAttribute("onclick", "printWorking()");
Run Code Online (Sandbox Code Playgroud)
以上工作完美无瑕.
以下所有都不会,并且不会将onclick传递给div.
divTag.onclick=printWorking;
divTag.onclick="printWorking";
divTag.onclick="printWorking()";
Run Code Online (Sandbox Code Playgroud)
还尝试了多种其他变体.
我想这是一个奇怪的问题,但我已经尝试在ImageView上设置onClicklistener并且它已经工作了.但问题是用户无法感知到点击.我的意思是如果你有一些人在其他移动环境(如Apple iPhone)上工作,那么当我们点击其他环境中的图像时,它会对图像产生影响,以便用户可以理解图像已被点击.
我尝试使用setalpha方法设置alpha 但它不起作用.虽然同样的事情在onFocusListener实现上工作正常.some1可以建议一种不同的方式来修改点击图像...
我是android的新手,所以还没有学习简单动画的细微差别......如果有任何简单的动画我可以使用相同的那么请告诉我.
谢谢!
我有一个可点击的TextView,我想给它一些颜色.但我不知道怎么做.以下是我正在处理的两个文件中的相关代码片段:
TextView title = new TextView(this);
title.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
title.setTextColor(R.color.textcolor);
title.setText(titleLine);
title.setTypeface(null, Typeface.BOLD);
title.setClickable(true);
title.setId(idLine);
title.setFocusable(true);
title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* Irrelevant code */
}
});
Run Code Online (Sandbox Code Playgroud)
这是我的textcolor.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000"/> <!-- focused -->
<item android:color="#000000"/> <!-- default -->
</selector>
Run Code Online (Sandbox Code Playgroud)
当我通过键入title.setTextColor(R.color.textcolor)使用textcolor文件时; ,无论我是否按下它,文字颜色都会变成灰色.这很奇怪,因为我在所有颜色字段中都写了"#000000".
但是如果我删除了setTextColor代码,则将textView变为浅灰色,当我按下它时,它变为黑色.但那不是我想要的颜色.
那么,任何人都可以帮我解决这个问题吗?
只是为了澄清:我希望能够在文本正常,按下和聚焦时指定文本的颜色.
我有一个带按钮的自定义通知.要设置通知并使用事件OnClick on my button,我使用了以下代码:
//Notification and intent of the notification
Notification notification = new Notification(R.drawable.stat_notify_missed_call,
"Custom Notification", System.currentTimeMillis());
Intent mainIntent = new Intent(getBaseContext(), NotificationActivity.class);
PendingIntent pendingMainIntent = PendingIntent.getActivity(getBaseContext(),
0, mainIntent , 0);
notification.contentIntent = pendingMainIntent;
//Remoteview and intent for my button
RemoteViews notificationView = new RemoteViews(getBaseContext().getPackageName(),
R.layout.remote_view_layout);
Intent activityIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:190"));
PendingIntent pendingLaunchIntent = PendingIntent.getActivity(getBaseContext(), 0,
activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationView.setOnClickPendingIntent(R.id.button1,
pendingLaunchIntent);
notification.contentView = notificationView;
notificationManager.notify(CUSTOM_NOTIFICATION_ID, notification);
Run Code Online (Sandbox Code Playgroud)
使用此代码,我使用自定义布局自定义通知...但我无法单击按钮!每次我尝试单击按钮时,我都会单击整个通知,因此脚本会启动"mainIntent"而不是"activityIntent".
我在互联网上看到这段代码不适用于所有终端.我已经在模拟器和HTC Magic上尝试过但我总是遇到同样的问题:我无法点击按钮!
我的代码是对的?有人可以帮帮我吗?
谢谢,
西蒙娜
我正在制作一个非常简单的应用程序,您可以单击方形div来将其颜色从白色更改为黑色.但是,我遇到了麻烦.我想使用onClick函数允许用户点击一个方块来改变它的颜色,但它似乎不起作用.我尝试过使用跨度和空p标签,但这也不起作用.
这是有问题的代码:
var Box = React.createClass({
getInitialState: function() {
return {
color: 'white'
};
},
changeColor: function() {
var newColor = this.state.color == 'white' ? 'black' : 'white';
this.setState({
color: newColor
});
},
render: function() {
return (
<div>
<div
style = {{background: this.state.color}}
onClick = {this.changeColor}
>
</div>
</div>
);
}
});
Run Code Online (Sandbox Code Playgroud)
这是我在CodePen上的小项目的链接. http://codepen.io/anfperez/pen/RorKge
我想这样做的菜鸟方式
$('*').click(function(){...});
Run Code Online (Sandbox Code Playgroud)
但有没有办法可以捕获任何类型的点击,而无需为DOM中的每个对象注册一个监听器?
帮助会很棒.=)
如何在HTML或JavaScript中按钮的onclick方法调用两个方法?
我需要找到一种方法来确定是否通过鼠标单击或按键激活了链接.
<a href="" onclick="submitData(event, '2011-07-04')">Save</a>
Run Code Online (Sandbox Code Playgroud)
我们的想法是,如果他们使用鼠标点击链接,那么他们可以继续使用鼠标选择他们下一步做什么.但是如果他们在页面上标签并且他们选中了Save链接,那么我将打开然后下一行进行编辑(该页面就像一个电子表格,每行都可以使用ajax编辑).
我认为可以查询事件参数按下哪个鼠标按钮,但是当没有按下任何按钮时,答案为0,这与鼠标左键相同.他们认为我可以从事件中获取keyCode,但是它会以未定义的形式返回,所以我假设鼠标事件不包含该信息.
function submitData(event, id)
{
alert("key = "+event.keyCode + " mouse button = "+event.button);
}
Run Code Online (Sandbox Code Playgroud)
总是返回"key = undefined mouse button = 0"
你能帮我吗?
如何让JavaScript在具有ONCLICK事件的新WINDOW中打开当前图像.
<script>
function imgWindow() {
window.open("image") }
</script>
Run Code Online (Sandbox Code Playgroud)
HTML
<img src="pond1.jpg" height="150" size="150" alt="Johnson Pond" onclick="image()"> <-- Have JavaScript open this image with onclick.
<img src="pond2.jpg" height="150" size="150" alt="All-green Pond" onclick="image()"> <-- Have JavaScript open this image with onclick.
<img src="pond3.jpg" height="150" size="150" alt="Dutch Pond" onclick="image()"> <-- Have JavaScript open this image with onclick.
Run Code Online (Sandbox Code Playgroud) 我试图在matplotlib中实现一个简单的鼠标单击事件.我希望绘制一个图,然后使用鼠标选择集成的下限和上限.到目前为止,我能够将坐标打印到屏幕,但不能存储它们以供以后在程序中使用.我还想在第二次鼠标点击后退出与图形的连接.
下面是当前绘制然后打印坐标的代码.
我的问题:
如何将图中的坐标存储到列表中?即click = [xpos,ypos]
是否可以获得两组x坐标,以便在该部分线上进行简单的积分?
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10,10)
y = x**2
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
def onclick(event):
global ix, iy
ix, iy = event.xdata, event.ydata
print 'x = %d, y = %d'%(
ix, iy)
global coords
coords = [ix, iy]
return coords
for i in xrange(0,1):
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
Run Code Online (Sandbox Code Playgroud)