这应该很简单,但我遇到了麻烦.如何获取子元素的父div?
我的HTML:
<div id="test">
<p id="myParagraph">Testing</p>
</div>
Run Code Online (Sandbox Code Playgroud)
我的JavaScript:
var pDoc = document.getElementById("myParagraph");
var parentDiv = ??????????
Run Code Online (Sandbox Code Playgroud)
我会想document.parent
或parent.container
会工作,但我一直在not defined
犯错误.请注意,它pDoc
是定义的,而不是它的某些变量.
有任何想法吗?
PS我希望尽可能避免使用jQuery.
这个让我难过.
我需要在自定义布局类中调用activity方法.这个问题是我不知道如何从布局中访问活动.
public class ProfileView extends LinearLayout
{
TextView profileTitleTextView;
ImageView profileScreenImageButton;
boolean isEmpty;
ProfileData data;
String name;
public ProfileView(Context context, AttributeSet attrs, String name, final ProfileData profileData)
{
super(context, attrs);
......
......
}
//Heres where things get complicated
public void onClick(View v)
{
//Need to get the parent activity and call its method.
ProfileActivity x = (ProfileActivity) context;
x.activityMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
public class ProfileActivityActivity extends Activity
{
//In here I am creating multiple ProfileViews and adding them …
Run Code Online (Sandbox Code Playgroud) 我今天换了讲师,他说我用了一个奇怪的代码.(他说最好使用.equals
,当我问为什么时,他回答"因为它是!")
所以这是一个例子:
if (o1.equals(o2))
{
System.out.println("Both integer objects are the same");
}
Run Code Online (Sandbox Code Playgroud)
而不是我习惯的:
if (o1 == o2)
{
System.out.println("Both integer objects are the same");
}
Run Code Online (Sandbox Code Playgroud)
这两者之间有什么区别.为什么他的方式(使用.equals
)更好?
通过快速搜索找到了这个,但我无法理解这个答案:
我一直在一堆不同的论坛上徘徊,我似乎每次都会看到这种情况.这是一个非常初学的问题.
我通常定义一个程序
#include<string>
using namespace std;
string x;
Run Code Online (Sandbox Code Playgroud)
我看到一堆代码示例将字符串定义为
std::string.
Run Code Online (Sandbox Code Playgroud)
这样做的目的是什么?是好的做法还是有一些功能?
所以我最近才发现了TODO评论.对于那些不知道的人,大多数现代IDE都会在评论中识别这个单词,并将评论行标记为不同的颜色,以便它突出.
例如.
//Need to talk to Bill about refactoring this code
//TODO:Need to talk to Bill about refactoring this code
Run Code Online (Sandbox Code Playgroud)
您可能会注意到IDE中的Eclipse或Rubymine突出显示了TODO注释和一些其他很好的功能.
我的问题是:还有像TODO这样的其他关键词吗?这似乎是一种更好的方式,通过评论和一个应该在IDE中广为人知的功能与开发人员进行沟通.毕竟他们的目的是帮助发展..
这个问题是关于框架,Java和处理.
这个问题听起来很复杂,但实际上并非如此.我会尝试将其保持在最低限度.我正在迷宫游戏中创建一个小球,以便了解物理和渲染.到目前为止,这是一次很好的体验,但我打了一个砖墙.
我决定的总体布局是在AWT框架中包含PApplet并使框架关闭.之所以这样,是因为我被告知你一次只能拥有一个Papplet实例.
PApplet
是Processing中的一个Applet
类,一个渲染库.
我在这里有3节课,包括主要课程
public class Menu extends PApplet
{
//images and buttons
PImage background, playbtn1, playbtn2, hsbtn1, hsbtn2, abbtn1, abbtn2, exbtn1, exbtn2;
FBox pBtn, hBtn, eBtn;
FWorld menu;
//simple constructor
public Menu()
{
}
public void setup()
{
size(600, 400);
smooth();
Fisica.init(this);
menu = new FWorld();
//loading and placing images
background = loadImage("MenuAlt.jpg");
System.out.println(background);
playbtn1 = loadImage("play1.gif");
playbtn2 = loadImage("play2.gif");
hsbtn1 = loadImage("high1.gif");
hsbtn2 = loadImage("high2.gif");
exbtn1 = loadImage("exit1.gif");
exbtn2 = …
Run Code Online (Sandbox Code Playgroud) 简单的问题.
我正在尝试拉伸webview的内容,以便可以看到整个网页.即没有滚动.我查看了文档,但无法找到缩放控件和setinitialscale之外的任何方法.
在这种情况下,setinitialscale的问题在于它对不同站点的工作方式不同.
示例:1:维基百科将按预期加载,缩放范围足够远.2:谷歌只会显示页面的中心.
下面是我的代码片段
test1 = (WebView) findViewById(R.id.webview_test_1);
test2 = (WebView) findViewById(R.id.webview_test_2);
test3 = (WebView) findViewById(R.id.webview_test_3);
test1.getSettings().setJavaScriptEnabled(true);
test2.getSettings().setJavaScriptEnabled(true);
test3.getSettings().setJavaScriptEnabled(true);
test1.setInitialScale(12);
test2.setInitialScale(12);
test3.setInitialScale(12);
test1.loadUrl("http://developer.android.com/resources/tutorials/views/hello-tablelayout.html");
test2.loadUrl("http://www.wikipedia.org/");
test3.loadUrl("http://www.google.com");
Run Code Online (Sandbox Code Playgroud)
test1.getSettings().setDefaultZoom(ZoomDensity.FAR);
Run Code Online (Sandbox Code Playgroud)
这似乎是我正在尝试做的一种替代方案,但我不能让它放大到足够远.
太简单了.
以此ul为例.
<ul>
<li>HI THERE</li>
<br>
<li>
<p >ME </p>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
当Li标签的innerHtml为空时,Li将自己包裹到文本中.
P标签不会发生这种情况.我假设这是因为p是段落之前和之后通常有空格.
我的问题:
有什么办法可以删除吗?
我不知道这是允许的,甚至是不可能的.是否可以使用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)
还尝试了多种其他变体.
如何设置元素以具有多个类?
初步尝试:
element.setAttribute("class","class1","class2");
element.className="class1 , class2";
element.class="class1 , class2";
Run Code Online (Sandbox Code Playgroud)